Sicily 1176. Two Ends

题目: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.

题意:给出n个正整数排成一列,A和B轮流取数,只能取两端的数,最后取到的数字之和大的人获胜,A和B之间的差为分值。A可以自由选择策略,B采取贪心策略,每次都选择两端中较大的数,如果相等则取左边的数。问A赢B的分值最大为多少。n<=1000,且n为偶数


思路:经过这学期的多道动态规划题目的锻炼,显然这道题用动态规划可以求解。用一个二维数组dp[i][j]表示端点为i和j的时候,A比B多多少分。因为n为偶数,所以最后一次取数字的时候一定是B取的,此时i=j,所以dp[i][i]=-datas[i],当i=j的时候,dp的值为负的数组值。接下来是转移方程,如果i-j+1是偶数,那么轮到B取数字,B采取贪心策略,当datas[i]>=datas[j]的时候,B取左边的数,dp[i][j]=-datas[i]+dp[i+1][j],否则B取右边的数,dp[i][j]=-datas[j]+dp[i][j-1]。如果i-j+1是奇数,那么轮到A取数字,A是基于全局最优的策略,所以dp[i][j]=max(dp[i+1][j]+datas[i], dp[i][j-1]+datas[j])。最后遍历i和j更新dp数组。因为更新是从长度小到长度大,0到n-1这一段是最后更新的,所以i从n-1遍历到0,j从i+1遍历到n-1。


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int dp[1105][1105];
int datas[1105];

int main()
{
    int n;
    int count = 0;
    while (cin >> n && n)
    {
        count++;
        memset(dp, -1, sizeof(dp));
        for (int i = 0; i < n; i++)
            cin >> datas[i];
        for (int i = 0; i < n; ++i)
            dp[i][i] = -datas[i];
        for (int i = n-1; i >= 0; i--)
        {
            for (int j = i + 1; j < n; ++j)
            {
                if ((j - i + 1) % 2 == 0)
                {
                    dp[i][j] = max(dp[i + 1][j] + datas[i], dp[i][j - 1] + datas[j]);
                }
                if ((j - i + 1) % 2 == 1)
                {
                    if (datas[i] >= datas[j])
                        dp[i][j] = dp[i + 1][j] - datas[i];
                    else
                        dp[i][j] = dp[i][j - 1] - datas[j];
                }
            }
        }
        printf("In game %d, the greedy strategy might lose by as many as %d points.\n", count, dp[0][n - 1]);
    }

    return 0;
}    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值