您的位置:首页 > 其它

[sicily]1176. Two Ends

2014-09-15 15:29 661 查看
1176. Two Ends 
   
 Time Limit: 1sec    Memory Limit:64MBDescriptionIn 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 cardin 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 exampleshows: (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.InputThere 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 thatn is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.OutputFor 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 largerend. If there is a tie, remove the left end.Sample Input Copy sample input to clipboard
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.
#include <iostream>
#include <stdio.h>

using namespace std;

const int int_max = 2147483647;

int score[1010][1010];
int card[1010];

int dp(int start, int end)
{
if (start >= end)
return 0;

if (score[start][end] != int_max)
return score[start][end];

//take left
int leftScore, rightScore;
if (card[start+1] >= card[end])
leftScore = dp(start+2, end) + card[start] - card[start+1];
else
leftScore = dp(start+1, end-1) + card[start] - card[end];
//take right
if (card[start] >= card[end-1])
rightScore = dp(start+1, end-1) + card[end] - card[start];
else
rightScore = dp(start, end - 2) + card[end] - card[end-1];

score[start][end] = max(leftScore, rightScore);

return score[start][end];
}

int main()
{
int numOfCard;
scanf("%d", &numOfCard);
int runTimes = 0;
while(numOfCard > 0)
{
runTimes++;

for(int i = 1; i <= numOfCard; i++)
{
scanf("%d",&card[i]);
}

for (int i = 1; i <= numOfCard; i++)
for (int j = 1; j <= numOfCard; j++)
score[i][j] = int_max;

int maxlose = dp(1, numOfCard);
printf("In game %d, the greedy strategy might lose by as many as %d points.\n", runTimes, maxlose);

scanf("%d", &numOfCard);
}
return 0;
}
感想:
动态规划题,本来是直接递归的,速度太慢。需要记下求解过程的中间状态,避免重复计算提升速度。
注意题目中的描述:
 If there is a tie, removethe left end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sicily