HDU 6199 gems gems gems dp

来源:互联网 发布:mpu6050单片机电路图 编辑:程序博客网 时间:2024/05/01 14:31

题目链接:HDU 6199

gems gems gems

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


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
131 3 2
 

Sample Output
4
 

题意:有一排宝石,每次从左到右取k个,AB两人A先取,A取1或2个,之后每个人取k个后,另一人只能取k或k+1个,A想要差值大,B想要差值小,问取完后差值是多少。

题目分析:比赛时被博弈的思想套住了,既然找不到必胜必负态自然也无法找规律或者sg函数,实际上是dp的题,dp[i][j][k] 代表第i个人从第j个开始取,可取的是k或k+1。i取0或1,j取20000,k的最大值是sqrt(n*2)<=200,状态转移方程是dp[0][i][j]=区间和[i~i+j-1]+max(dp[1][i+j][j],dp[1][i+j+1][j+1]+s[i+j])对于另一个人也是一样,然后注意有时取不到k+1,之后网上get到的每次只会影响k区间范围的数,所以滚动数组即可,滚动的大小比k最大值大即可,这里取256,%256可以用&255来优化。

////  main.cpp//  gems gems gems////  Created by teddywang on 2017/09/10.//  Copyright © 2017年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int T;int dp[2][300][300];int n;int s[21001],pre[21001];int mod=255;int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        memset(dp,0,sizeof(dp));        pre[0]=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&s[i]);            pre[i]=pre[i-1]+s[i];        }        int size=sqrt(n*2)+1;        for(int i=n;i>=1;i--)        {            for(int j=size;j>=1;j--)            {                if(i+j<=n)                {                    dp[0][i&mod][j]=pre[(i+j-1)]-pre[i-1]+max(dp[1][(i+j)&mod][j],dp[1][(i+j+1)&mod][j+1]+s[i+j]);                    dp[1][i&mod][j]=-pre[i+j-1]+pre[i-1]+min(dp[0][(i+j)&mod][j],dp[0][(i+j+1)&mod][j+1]-s[i+j]);                }                else if(i+j==n+1)                {                    dp[0][i&mod][j]=pre[i+j-1]-pre[i-1]+dp[1][(i+j)&mod][j];                    dp[1][i&mod][j]=pre[i-1]-pre[i+j-1]+dp[0][(i+j)&mod][j];                }            }        }        printf("%d\n",dp[0][1][1]);    }        }

原创粉丝点击