HDU 6199 gems gems gems dp

7 篇文章 0 订阅

题目链接: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
  
  
1 3 1 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]);
    }
    
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值