zoukankan      html  css  js  c++  java
  • HDU 6199 DP

    gems gems gems

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


    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
     

     题意:

    Alice 和 Bob 玩游戏,有n堆宝石,每一堆都有价值,如果前一个人拿了k堆那么下一个人能够拿k堆或者k+1堆,两个人都希望自己拿到的价值与另一个人的差最大,求最后价值Alice-Bob,Alice可以先拿1堆或者2堆。

    代码:

    //看似博弈其实是dp,由于从第一个人开始并且他只能拿1或2堆,所以这是终结状态,从后向前推,设f[0/1][i][j]表示第1/2个人从第i堆开始拿并
    //且上一个人拿了j堆的最优方案(Alice拿到的-Bob拿到的),每一个人又有两种拿法即j,j+1,所以处理一下前缀和转移方程就出来了。两个人都希
    //望最优即第一个人希望(Alice-Bob)最大,第二个人希望(Bob-Alice)最大,我们可以把后一项看成(Alice-Bob)最小。
    //因为k最大不超过200(2+4+...+k<=n),但是数组还是太大,要滚动数组。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MOD=223;
    int f[2][233][209],sum[20009];
    int main()
    {
        int t,n;
        scanf("%d",&t);
        while(t--){
            sum[0]=0;
            memset(f,0,sizeof(f));
            scanf("%d",&n);
            for(int i=1;i<=n;i++){
                scanf("%d",&sum[i]);
                sum[i]+=sum[i-1];
            }
            for(int i=n;i>=1;i--){
                for(int j=1;j<=200;j++){
                    if(i+j<=n){
                        f[0][i%MOD][j]=max(sum[i+j-1]-sum[i-1]+f[1][(i+j)%MOD][j],sum[i+j]-sum[i-1]+f[1][(i+j+1)%MOD][j+1]);
                        f[1][i%MOD][j]=min(sum[i-1]-sum[i+j-1]+f[0][(i+j)%MOD][j],sum[i-1]-sum[i+j]+f[0][(i+j+1)%MOD][j+1]);
                    }else if(i+j-1<=n){
                        f[0][i%MOD][j]=sum[i+j-1]-sum[i-1]+f[1][(i+j)%MOD][j];
                        f[1][i%MOD][j]=sum[i-1]-sum[i+j-1]+f[0][(i+j)%MOD][j];
                    }
                }
            }
            printf("%d
    ",f[0][1][1]);
        }
        return 0;
    }
  • 相关阅读:
    字符串匹配的三种算法
    字符串匹配
    多少个矩形被覆盖
    指针和地址的区别
    股票开盘的最大成交额-----一道不错的贪心算法题目
    Z字形扫描矩阵
    第几次出现
    节日(CCF试题)
    数字排序
    JS对象添加新的字段
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7615122.html
Copyright © 2011-2022 走看看