您的位置:首页 > 其它

Sicily 1176. Two Ends

2014-09-27 15:11 363 查看



1176. Two Ends


Constraints

Time Limit: 1 secs, Memory Limit: 64 MB


Description

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.


Input

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.


Output

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.


Sample Input


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


Sample Output


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.


</pre><pre name="code" class="cpp">#include <iostream>
#include <memory.h>
using namespace std;

//maxPoints[i][j]记录剩下第i个~第j个数字的时候,玩家一得到的最多的分数
int maxPoints[1001][1001];

//获得maxPoints[i][j]
int pick (int *num, int start, int end);

int main() {
int t = 0;
int n;
while (cin >>n && n != 0) {
memset(maxPoints, 0, sizeof(maxPoints));
int *num = new int
;
int sum = 0;
for (int i = 0; i < n; i ++) {
cin >>num[i];
sum += num[i];
}
int max = pick(num, 0, n-1);
int diff = 2*max - sum;
cout <<"In game " <<++t <<", the greedy strategy might lose by as many as " <<diff <<" points." <<endl;

}
return 0;
}

int pick (int *num, int start, int end) {
if (maxPoints[start][end] == 0) {
if (start + 1 == end) {
return maxPoints[start][end] = num[start] > num[end] ? num[start] : num[end];
} else if (start == end) {
// 如果一共有奇数个数字,虽然题目没有要求,也顺便考虑这种情况
return maxPoints[start][end] = num[start];
}

int leftSelected, rightSelected;

// 玩家一先取左边, 如果剩下的两端左右相等,玩家二先取左端
if (num[start+1] >= num[end]) {
leftSelected = pick(num, start+2, end) + num[start];
} else {
leftSelected = pick(num, start+1, end-1) + num[start];
}

// 玩家一先取右边, 如果剩下的两端左右相等,玩家二先取左端
if (num[start] >= num[end-1]) {
rightSelected = pick(num, start+1, end-1) + num[end];
} else {
rightSelected = pick(num, start, end-2) + num[end];
}

maxPoints[start][end] = leftSelected > rightSelected ? leftSelected : rightSelected;
}
return maxPoints[start][end];
}


Run Time 0.02sec

Run Memory 4228 KB

未做特别的优化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: