hdu 6199 gems gems gems dp

本文介绍了一个涉及宝石收集的游戏策略问题,Alice与Bob轮流从一排宝石中取走一定数量的宝石,目标是使自己获得的宝石总价值最大化。通过动态规划算法解决了此问题,并给出了具体的实现代码。

gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)



Problem Description
Now there are  n gems, each of which has its own value. Alice and Bob play a game with these  n gems.
They place the gems in a row and decide to take turns to take gems from left to right. 
Alice goes first and takes 1 or 2 gems from the left. After that, on each turn a player can take  k or  k+1 gems if the other player takes  k gems in the previous turn. The game ends when there are no gems left or the current player can't take  k or  k+1 gems.
Your task is to determine the difference between the total value of gems Alice took and Bob took. Assume both players play optimally. Alice wants to maximize the difference while Bob wants to minimize it.
 

 

Input
The first line contains an integer  T ( 1T10), the number of the test cases. 
For each test case:
the first line contains a numbers  n ( 1n20000);
the second line contains n numbers:  V1,V2Vn. ( 100000Vi100000)
 

 

Output
For each test case, print a single number in a line: the difference between the total value of gems Alice took and the total value of gems Bob took.
 

 

Sample Input
1 3 1 3 2
 

 

Sample Output
4
 

 

Source

思路:dp,蜜汁题意;滚动数组优化空间;

#include<bits/stdc++.h>
using namespace std;

const int N=2e4+10,M=2e6+10,inf=1e9+10;

int dp[2][1010][201],n,sum[N];

int main()
{
    int T,x;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&x),sum[i]=sum[i-1]+x;
        for(int i=n;i>=1;i--)
        {
            for(int j=200;j>=1;j--)
            {
                if(i+j<=n)
                {
                    dp[0][i%1000][j]=max(sum[i+j-1]-sum[i-1]+dp[1][(i+j)%1000][j],sum[i+j]-sum[i-1]+dp[1][(i+j+1)%1000][j+1]);
                    dp[1][i%1000][j]=min(-sum[i+j-1]+sum[i-1]+dp[0][(i+j)%1000][j],-sum[i+j]+sum[i-1]+dp[0][(i+j+1)%1000][j+1]);
                }
                else if(i+j-1<=n)
                {
                    dp[0][i%1000][j]=dp[1][(i+j)%1000][j]+sum[i+j-1]-sum[i-1];
                    dp[1][i%1000][j]=dp[0][(i+j)%1000][j]-sum[i+j-1]+sum[i-1];
                }
            }
        }
        printf("%d\n",dp[0][1][1]);
    }
    return 0;
}

 

 

gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 625    Accepted Submission(s): 77


Problem Description
Now there are  n gems, each of which has its own value. Alice and Bob play a game with these  n gems.
They place the gems in a row and decide to take turns to take gems from left to right. 
Alice goes first and takes 1 or 2 gems from the left. After that, on each turn a player can take  k or  k+1 gems if the other player takes  k gems in the previous turn. The game ends when there are no gems left or the current player can't take  k or  k+1 gems.
Your task is to determine the difference between the total value of gems Alice took and Bob took. Assume both players play optimally. Alice wants to maximize the difference while Bob wants to minimize it.
 

 

Input
The first line contains an integer  T ( 1T10), the number of the test cases. 
For each test case:
the first line contains a numbers  n ( 1n20000);
the second line contains n numbers:  V1,V2Vn. ( 100000Vi100000)
 

 

Output
For each test case, print a single number in a line: the difference between the total value of gems Alice took and the total value of gems Bob took.
 

 

Sample Input
1 3 1 3 2
 

 

Sample Output
4
 

 

Source

转载于:https://www.cnblogs.com/jhz033/p/7505091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值