sicily 1176. Two Ends

来源:互联网 发布:温州平阳网络问政平台 编辑:程序博客网 时间:2024/05/18 03:05

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 48 1 2 3 4 5 6 7 88 2 2 1 5 3 8 7 30

 

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.

 

 

解题思路

1、动态规划+记忆化搜索

动态规划方程:

dp[s][t] : 表示从第s到第t张牌,先手最多可以得多少点数。

dp[s][t] = max(

                      card[s+1] >=card[t] ? card[s] + dp[s+2][t] : card[s] + dp[s+1][t-1]

                      (先手取左边,接下来card[s+1] >=card[t],后手取左边; 先手取左边,接下来card[s+1]<card[t],后手取右边) ,

                      card[s]>=card[t-1]? card[t] + dp[s+1][t-1] :   card[t] + dp[s][t-2]  

                      (先手取右边,接下来 card[s]>=card[t-1],后手取左边; 先手取右边,接下来card[s]<card[t-1],后手取右边)        ,

                        );

记忆化搜索:

由于在计算dp[0][n-1]的过程中,使用循环去做的话很麻烦,可以考虑使用递归去做,在求解dp[0][n-1]的时候转化为求解更小的问题,小问题的结果保存在数组dp中。

 

代码如下:

 

 

#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 1010;int dp[maxn][maxn];int card[maxn];int n;int dp_solve(int s, int t);int case_num = 0;int sum;int main(){while (cin >> n&&n){case_num++;sum = 0;for (int i = 0; i < n; i++){cin >> card[i];sum += card[i];for (int j = 0; j < n; j++){dp[i][j] = -1;}dp[i][i] = card[i];}int ans = dp_solve(0, n - 1);printf("In game %d, the greedy strategy might lose by as many as %d points.\n", case_num, 2 * ans - sum);}}int dp_solve(int s, int t){if (s > t)return 0;else if (dp[s][t] != -1)returndp[s][t];else{dp[s][t] = max(card[s] + (card[s + 1] >= card[t] ? dp_solve(s + 2, t) : dp_solve(s + 1, t - 1)),card[t] + (card[s] >= card[t - 1] ? dp_solve(s + 1, t - 1) : dp_solve(s, t - 2)));return dp[s][t];}}


 

 

 

0 0