题目来源:http://soj.me/1176
Time Limit: 1 secs, Memory Limit: 64 MB
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.
思路:刚刚开始就走进一个误区,以为是找规律,从而推测出判断偶数位牌和奇数位牌的差,就是所要求的,结果WA了。后来,觉得确实不妥,这不是最大的。就采用动规了。largestScroe[a][b]记录从第a张牌到第b张牌开始挑选,可以取得的最大分数。(对方用贪心)这样就有了dp函数中的判断。只有两种情况,拿左边的或者拿右边的,找出最大值即可。
代码:
/* Link: http://soj.me/1176 Author: BetaBin Date: 2012/07/25 */ #include <stdio.h> int cards[1005]; int largestScore[1005][1005]; int cardNum; int cardSum; int gameId; int i,j; int bestScore; int dp(int begin, int end) { int situation_left; int situation_right; if(end - begin <= 1) { return (cards[begin] > cards[end]) ? cards[begin] : cards[end]; } if(-1 != largestScore[begin][end]) { return largestScore[begin][end]; } if(cards[begin + 1] >= cards[end]) { situation_left = cards[begin] + dp(begin + 2, end); } else { situation_left = cards[begin] + dp(begin + 1, end - 1); } if(cards[begin] >= cards[end - 1]) { situation_right = cards[end] + dp(begin + 1, end - 1); } else { situation_right = cards[end] + dp(begin, end - 2); } largestScore[begin][end] = (situation_left > situation_right) ? situation_left : situation_right; return largestScore[begin][end]; } int main() { gameId = 0; while(scanf("%d", &cardNum) && 0 != cardNum) { ++gameId; cardSum = 0; for(i = 1; i <= cardNum; ++i) { scanf("%d", &cards[i]); cardSum += cards[i]; for(j = 1; j < i; ++j) { largestScore[j][i] = -1; } } bestScore = dp(1, cardNum); printf("In game %d, the greedy strategy might lose by as many as %d points.\n", gameId, bestScore + bestScore - cardSum); } return 0; }