2013
11-11

# 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;

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;
}
}

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;
}