[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 <= 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.
输入输出样例
5
1
3
1
7
2
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]为还剩下i个硬币,上次取走j个硬币的最大值。
显然f[i][j] = max(sum[i] - f[i-k][k]);
这样是O(N^3)的;
我们考虑f[i][j]与f[i][j-1]相差在哪了。
就是2 * j和2 * j - 1.
于是让f[i][j]=f[i][j-1],然后判断2*j和2*j-1更新就行了。
// luogu-judger-enable-o2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
int res=;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) res=(res<<)+(res<<)+(ch^),ch=getchar();
return res;
}
#define reg register int n;
int a[], qzh[];
int f[][]; int main()
{
n = read();
for (reg int i = ; i <= n ; i ++) a[n - i + ] = read();
for (reg int i = ; i <= n ; i ++) qzh[i] = qzh[i-] + a[i];
for (reg int i = ; i <= n ; i ++)
{
for (reg int j = ; j <= n ; j ++)
{
f[i][j] = f[i][j-];
int k = (j << ) - ;
if (k <= i) f[i][j] = max(f[i][j], qzh[i] - f[i-k][k]);
k++;
if (k <= i) f[i][j] = max(f[i][j], qzh[i] - f[i-k][k]);
}
}
printf("%d\n", f[n][]);
return ;
}
[LUOGU2964] [USACO09NOV]硬币的游戏A Coin Game的更多相关文章
- [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 c ...
- 洛谷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 c ...
- [USACO09NOV]硬币的游戏A Coin Game
https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i- ...
- LUOGU 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 c ...
- P2964 [USACO09NOV]硬币的游戏A Coin Game (DP)
题意:n颗硬币 两个人从前往后按顺序拿 如果上一个人拿了i颗 那么下一个可以拿1-2*i颗 问先手能获得的最大收益 题解:比较典型的最大最小最大最小..DP了 但是暴力做的话是n^3 所以就体现出了这 ...
- [USACO09NOV]硬币的游戏 博弈 dp
LINK : coin game 这道题 超级经典去年这个时候我就看过题目了 但时至今日还不会/cy 觉得在做比赛的题目的时候少写省选的题目 多做水题多做不难也不简单的题目就好了. 由于我是真的不会博 ...
- 【P2964】硬币的游戏(DP+前缀和)
一道DP,思维难度真是不小. 首先对于这个题的数据,我们可以发现差不多可以支持n^2logn,但是貌似也不会有这种复杂度的线性DP(至少这个题看上去不是这样).所以我们考虑N^2做法.因为求得是价值和 ...
- 洛谷 [P2964] 硬币的游戏
博弈论+dp 依旧是博弈论的壳子,但问的是最大值,所以要dp 设 dp[i][j] 表示该取 i 号硬币,上一次取了 j 个的先手能取的最大值, 因为每次从小到大枚举复杂度太高,所以我们要从 dp[i ...
- 题解 SP5271 XOINC - A Coin Game
SP5271 XOINC - A Coin Game 双倍经验:P2964 [USACO09NOV]硬币的游戏A Coin Game O3做法(TLE):枚举i,j,k,即剩下i枚金币,上一轮选了j枚 ...
随机推荐
- 自定义JDBC工具类(配置文件)
import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql. ...
- 在IIS上启用WordPress子域名模式多站点功能
昨天负责网站的支持人员向我反馈在我们负责托管网站的WordPress在启动多站点功能后,浏览新站点或访问新站点的"Dashboard"时,都会反馈“404”错误.我检查了WordP ...
- LoadRunner11.安装破解
Loadrunner安装及破解 一. 安装 1. 将ISO文件导入,打开光驱,运行“setup.exe” 2. 点击安装,部分机器会提示缺少“Microsoft Visual C++ 2005 S ...
- pt-archiver归档数据 源库和目标库是否会出现不一致
背景 归档的表在源库和目标库都要存在 pt-archiver归档表的场景有:不删原表数据,非批量插入目标库:不删原表数据,批量插入目标库:非批量删除原表数据,非批量插入目标库:批量删除原表数据,批量插 ...
- 面试官:都说阻塞 I/O 模型将会使线程休眠,为什么 Java 线程状态却是 RUNNABLE?
摘要: 原创出处 https://studyidea.cn 「公众号:程序通事 」欢迎关注和转载,保留摘要,谢谢! 使用 Java 阻塞 I/O 模型读取数据,将会导致线程阻塞,线程将会进入休眠,从而 ...
- 命名对象继承2-验证Open*命名对象安全属性的传递
接上一篇 这次是验证Open*(本文使用OpenMutex函数)的命名对象在继承中安全属性的传递 SECURITY_ATTRIBUTES sa; //设置句柄安全性 sa.nLength = size ...
- 『王霸之路』从0.1到2.0一文看尽TensorFlow奋斗史
0 序篇 2015年11月,Google正式发布了Tensorflow的白皮书并开源TensorFlow 0.1 版本. 2017年02月,Tensorflow正式发布了1.0.0版本,同时也标志 ...
- Maven私服Nexus的搭建
# Maven私服Nexus的搭建 ## 私服存在的合理性 Maven中的依赖是从服务器仓库中下载的,Maven的仓库只有两大类: - 1) 本地仓库 - 2) 远程仓库,其中在远程仓库中又分成了3种 ...
- 快学Scala 第二十课 (trait的构造顺序)
trait的构造顺序: 首先调用超类构造器 特质构造器在超类构造器之后,类构造器之前执行 特质从左向右被构造 每个特质当中,父特质先被构造 如果多个特质共有一个父特质,而那个父特质已经被构造,则不会被 ...
- Linux下的软件管理
一.软件包管理简介 1源码包(脚本安装) ①.特点: a.开源 ,可以修改 b.自由选择所需功能 ...