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 eitherend 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 indicatesend 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
未做特别的优化。
相关文章推荐
- sicily 1176 Two Ends
- [Sicily 1176 Two Ends] 动态规划 记忆化搜索
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
- sicily 1176 Two ends
- sicily 1176. Two Ends
- Sicily 1176 Two Ends
- Sicily 1176 Two Ends
- sicily 1176 two ends
- sicily 1176.Two ends
- [sicily]1176. Two Ends
- sicily 1176. Two Ends
- Sicily 1176. Two Ends
- Sicily 1176. Two Ends
- sicily 1176 Two Ends
- sicily 1176 two ends 动态规划解题
- Sicily 1176. Two Ends
- soj 1176. Two Ends
- 1176 Two Ends
- 1176. Two Ends(动态规划,两端取数 )
- sicily 1176