洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game

题目描述

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).、



和那道game of sum很想

不过要约束选几个

f[i][j]表示剩下1到i,上一人选了j个,选手得分的最大值

暴力转移枚举f[i-k][k],发现f[i][j]只比f[i][j-1]多了两个转移,可以优化

关于博弈:f[i][j]是1号先手的话,自然选择s[i]-f[i-k][k]最大的,这里面的f[i-k][k]是2号先手

答案就是f[n][1],全剩下,1号先手最大,可以选一个或两个

PS:注意c要倒着

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=2e3+5;
typedef long long ll;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
int n,c[N],s[N],f[N][N];
void dp(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            f[i][j]=f[i][j-1];
            int k=2*j;
            if(i-k>=0) f[i][j]=max(f[i][j],s[i]-f[i-k][k]);
            k=2*j-1;
            if(i-k>=0) f[i][j]=max(f[i][j],s[i]-f[i-k][k]);
        }
    }
}
int main(){
    n=read();
    for(int i=n;i>=1;i--) c[i]=read();
    for(int i=1;i<=n;i++) s[i]=s[i-1]+c[i];
    dp();
    printf("%d",f[n][1]);
}
时间: 2024-09-29 23:34:29

洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game的相关文章

P2964 [USACO09NOV]硬币的游戏A Coin Game

题目描述 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 <=

[USACO09NOV]硬币的游戏A Coin Game

https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i-k][k] dp[i][j]=max(sum[i]-dp[i-k][k]) 因为 上一次取走j个和取走j-1个 k的取值范围 只相差 2*j-1 和 2*j 所以 直接 dp[i][j]=dp[i][j-1] 然后k分别等于  2*j-1 和 2*j,转移 最后输出dp[n][1],因为先手可以拿1

[luogu2964][USACO09NOV][硬币的游戏A Coin Game]

题目描述 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 <=

洛谷P1450 [HAOI2008]硬币购物 动态规划 + 容斥原理

洛谷P1450 [HAOI2008]硬币购物 动态规划 + 容斥原理 1.首先我们去掉限制 假设 能够取 无数次 也就是说一开始把他当做完全背包来考虑 离线DP 预处理 复杂度 4*v 用f[ i ] 表示 空间 为 i 的方案数 答案ans 其实就是所有方案 - 所有超过限制的方案 限制指的就是题目中限制 某个硬币有几枚 然后所有超过限制的方案用容斥来做 所有超过限制的方案 要减 == -1 超过限制的方案 - 2 超过限制的方案 - 3 超过限制的方案 - 4 超过限制的方案 + 1和2 超

洛谷 P1005 矩阵取数游戏

题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2.每次取走的各个元素只能是该元素所在行的行首或行尾: 3.每次取数都有一个得分值,为每行取数的得分之和,每行取数的得分 = 被取走的元素值*2^i,其中i表示第i次取数(从1开始编号): 4.游戏结束总得分为m次取数得分之和. 帅帅想请你帮忙写一个程序,对于任意矩阵,可以求出取数后的最大得分. 输入输

洛谷 [P2964] 硬币的游戏

博弈论+dp 依旧是博弈论的壳子,但问的是最大值,所以要dp 设 dp[i][j] 表示该取 i 号硬币,上一次取了 j 个的先手能取的最大值, 因为每次从小到大枚举复杂度太高,所以我们要从 dp[i][i - 1] 转移,每次新加两个状态即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using n

洛谷 P2962 [USACO09NOV]灯Lights

P2962 [USACO09NOV]灯Lights 题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were all turned off. Help the cows get all the lights back on so they can resume their games. The N (1 <= N <= 35) lights conve

洛谷P1450 [HAOI2008]硬币购物

题目描述 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. 输入输出格式 输入格式: 第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s 输出格式: 每次的方法数 输入输出样例 输入样例#1: 1 2 5 10 2 3 2 3 1 10 1000 2 2 2 900 输出样例#1: 4 27 说明 di,s<=100000 tot<=1000 [HAOI2

洛谷 P1005 矩阵取数游戏 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=1005 题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2.每次取走的各个元素只能是该元素所在行的行首或行尾: 3.每次取数都有一个得分值,为每行取数的得分之和,每行取数的得分 = 被取走的元