• 【题解】Luogu p2964 BZOJ 2017[Usaco2009 Nov]硬币游戏


    题目描述

    Farmer John’s cows like to play coin games so FJ has invented with a new two-player coin game called Xoinc for them.

    Initially a stack of N (5 <= N <= 2,000) coins sits on the ground; coin i from the top has integer value C_i (1 <= C_i <= 100,000).

    The first player starts the game by taking the top one or two coins (C_1 and maybe C_2) from the stack. If the first player takes just the top coin, the second player may take the following one or two coins in the next turn. If the first player takes two coins then the second player may take the top one, two, three or four coins from the stack. In each turn, the current player must take at least one coin and at most two times the amount of coins last taken by the opposing player. The game is over when there are no more coins to take.

    Afterwards, they can use the value of the coins they have taken from the stack to buy treats from FJ, so naturally, their purpose in the game is to maximize the total value of the coins they take. Assuming the second player plays optimally to maximize his own winnings, what is the highest total value that the first player can have when the game is over?

    MEMORY LIMIT: 20 MB

    农夫约翰的奶牛喜欢玩硬币游戏.

    初始时,一个有N枚硬币的堆栈放在地上,从堆顶数起的第i枚硬币的币值 为Ci

    开始玩游戏时,第一个玩家可以从堆顶拿走一枚或两枚硬币.如果第一个玩家只拿走堆顶的 一枚硬币,那么第二个玩家可以拿走随后的一枚或两枚硬币.如果第一个玩家拿走两枚硬币,则第二个玩家可以拿走1,2,3,或4枚硬币.在每一轮中,当前的玩家至少拿走一枚硬币,至多拿 走对手上一次所拿硬币数量的两倍.当没有硬币可拿时,游戏结束.

    两个玩家都希望拿到最多钱数的硬币.请问,当游戏结束时,第一个玩家最多能拿多少钱 呢?

    输入输出格式

    输入格式:

    • Line 1: A single integer: N

    • Lines 2..N+1: Line i+1 contains a single integer: C_i

    输出格式:

    • Line 1: A single integer representing the maximum value that can be made by the first player.

    输入输出样例

    输入样例#1: 复制

    5 
    1 
    3 
    1 
    7 
    2
    

    输出样例#1: 复制

    9
    

    说明

    There are five coins with the values 1, 3, 1, 7, and 2.

    The first player starts by taking a single coin (value 1). The opponent takes one coin as well (value 3). The first player takes two more coins (values 1 and 7 — total 9). The second player gets the leftover coin (value 2— total 5).

    思路

    用$f[i][j]$表示上一个人拿了j个,还剩1~i个时,能拿到的最大硬币数;

    • 枚举现在要拿k个
      转移方程$$f[i][j]=max(f[i][j],sum[i]-f[i-k][k])$$
      因为$f[i][j-1]$已经算了一大片了,所以k只要枚举$j*2-1$和$j*2$就可以
      答案就是$f[n][1]$

    代码

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #define re register int
     8 using namespace std;
     9 const int maxn=2e3+10;
    10 inline int read(){
    11     int x=0,w=1;
    12     char ch=getchar();
    13     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    14     if(ch=='-') w=-1,ch=getchar();
    15     while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
    16     return x*w;
    17 }
    18 int n,sum[maxn],c[maxn],dp[maxn][maxn];
    19 int main(){
    20     freopen("p2964.in","r",stdin);
    21     freopen("p2964.out","w",stdout);
    22     n=read();
    23     for(re i=n;i>=1;--i) c[i]=read();
    24     for(re i=1;i<=n;++i) sum[i]+=sum[i-1]+c[i];
    25     for(re i=1;i<=n;++i)
    26         for(re j=1;j<=n;++j){
    27                dp[i][j]=dp[i][j-1];
    28             int k=2*j-1;
    29             if(k<=i)  dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
    30             k++;
    31             if(k<=i)  dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
    32         }
    33     printf("%d
    ",dp[n][1]);
    34     return 0;
    35 }
    c++
    #include<cmath>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define re register int
    using namespace std;
    const int maxn=2e3+10;
    inline int read(){
        int x=0,w=1;
        char ch=getchar();
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') w=-1,ch=getchar();
        while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
        return x*w;
    }
    int n,sum[maxn],c[maxn],dp[maxn][maxn];
    int main(){
        freopen("p2964.in","r",stdin);
        freopen("p2964.out","w",stdout);
        n=read();
        for(re i=n;i>=1;--i) c[i]=read();
        for(re i=1;i<=n;++i) sum[i]+=sum[i-1]+c[i];
        for(re i=1;i<=n;++i)
            for(re j=1;j<=n;++j){
                   dp[i][j]=dp[i][j-1];
                int k=2*j-1;
                if(k<=i)  dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
                k++;
                if(k<=i)  dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]);
            }
        printf("%d
    ",dp[n][1]);
        return 0;
    }
    
  • 相关阅读:
    requests
    Py西游攻关之Socket网络编程
    编码的真相
    where查询条件的字段顺序打乱会影响命中索引吗?
    mysql联合索引怎样才会命中
    mysql执行计划解释
    mysql模糊查询(%)或范围查询怎样会影响命中索引
    完全符合能量守恒,胡克定理的JavaScript弹簧振子(超简洁版,超越第一版!)
    高仿真可控弹簧振子JavaScript编写
    JavaScript 3D, Cube,3D 立方体
  • 原文地址:https://www.cnblogs.com/bbqub/p/8515540.html
Copyright © 2020-2023  润新知