首页 > 专题系列 > Java解POJ > POJ 2738 Two Ends [解题报告] Java
2013
11-11

POJ 2738 Two Ends [解题报告] Java

Two Ends

问题描述 :

In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)

3 2 10 4

You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

输入:

There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

输出:

For each test case you should print one line of output of the form:

In game m, the greedy strategy might lose by as many as p points.

where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

样例输入:

4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

样例输出:

In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

解题代码:

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;

public class Main {
 static final int M = 2,N = 1000+2;
 static int value[] = new int[N];
 static int map[][][] = new int[N][N][M],n;
 public static int Get_Num(StreamTokenizer cin) throws Exception{
	cin.nextToken();
	return (int) cin.nval;
 }

 public static void main(String []args) throws Exception{
		
  int i,k=1;
		
  StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  while(true){
	n = Get_Num(cin);
	if(n==0) break;
		
	for(i=1;i<=n;++i) value[i] = Get_Num(cin);
		
System.out.println("In game "+k+", the greedy strategy might lose by as many as "+solve()+" points.");
	++k;
  }
}

 public static void init_map(){
	int i,j;
	for(i=0;i<=n;++i) for(j=0;j<=n;++j){
		map[i][j][0] = map[i][j][1] = -1;
	}
  }

 public static int max(int a,int b){
	return a>b?a:b;
 }

 public static int get_ans(int left,int right,int p){
  if(map[left][right][p]!=-1)
	return map[left][right][p];
  if(left==right){
	if(p==1) return map[left][right][p] = value[left];
	return map[left][right][p] = 0;
  }
  if(p==1){
return map[left][right][p] = max(get_ans(left,right-1,1-p)+value[right],get_ans(left+1,right,1-p)+value[left]);
  }else{
return map[left][right][p] = (value[right]>value[left]?get_ans(left,right-1,1-p):get_ans(left+1,right,1-p));
 }
}

 public static int solve(){
	int i,k=0;
	init_map();
	for(i=1;i<=n;++i) k+=value[i];
	return get_ans(1,n,1)*2-k;
  }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。