[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.
输入输出样例
说明
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).
题解(来自湖南16培训)
不难想到,此题的状态转移方程为:f[i][j+1]=s[i]-min{f[i-k][k]}(k<=(j+1)*2)
但由于部分f[i-k][k]在之前的动规中已经计算过了,而对当前产生影响的就只有两个操作,即取j×2或j×2-1个
因此可以优化调一层枚举,得到方程为:f[i][j+1]=max(f[i][j],s[i]-min{f[i-(j*2+1)][j*2+1],f[i-(j*2+2)][j*2+2]}
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=;
inline int read(){
int x=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))
f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
int n,a[N],sum[N],f[N][N];
int main(){
n=read();
for(int i=n;i>=;i--)
sum[i]=read();
for(int i=;i<=n;i++)
sum[i]+=sum[i-];
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
f[i][j]=f[i][j-];
if(i>=(j<<)-){
f[i][j]=max(f[i][j],sum[i]-f[i-(j<<)+][(j<<)-]);
if(i>=(j<<))
f[i][j]=max(f[i][j],sum[i]-f[i-(j<<)][j<<]);
}
}
printf("%d\n",f[n][]);
return ;
}
orz thwfhk
[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 觉得在做比赛的题目的时候少写省选的题目 多做水题多做不难也不简单的题目就好了. 由于我是真的不会博 ...
- POJ.1067 取石子游戏 (博弈论 威佐夫博弈)
POJ.1067 取石子游戏 (博弈论 威佐夫博弈) 题意分析 简单的威佐夫博弈 博弈论快速入门 代码总览 #include <cstdio> #include <cmath> ...
- hdu 3537 Daizhenyang's Coin(博弈-翻硬币游戏)
题意:每次可以翻动一个.二个或三个硬币.(Mock Turtles游戏) 初始编号从0开始. 当N==1时,硬币为:正,先手必胜,所以sg[0]=1. 当N==2时,硬币为:反正,先手必赢,先手操作后 ...
- HDU 3537 Daizhenyang's Coin(博弈,翻硬币)
Daizhenyang's Coin Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- IDE有毒
程序员按项目性质大致有三种:写Demo的.写Proto的.写成品的:按项目开发周期大致有:写开头的.写中间的.写结尾的. Demo是样品,主要是表面上初步实现,临时忽悠客户用的,不一定要求继续演化: ...
- SSH整合(struts2.3.24+hibernate3.6.10+spring4.3.2+mysql5.5+myeclipse8.5+tomcat6+jdk1.6)
终于开始了ssh的整合,虽然现在比较推崇的是,ssm(springmvc+spring+mybatis)这种框架搭配确实比ssh有吸引力,因为一方面springmvc本身就是遵循spring标准,所以 ...
- ExpandableListView实现展开更多和收起更多
[需求]: 如上面图示 当点开某个一级菜单的时候,其他菜单收起: 子级菜单默认最多5个: 多于5个的显示"展开更多" 点击"展开更多",展开该级所有子级菜单,同 ...
- C语言中把数字转换为字符串 【转】
在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出 ...
- js限制文本框只能输入数字方法小结
有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等代码. 例如,输入大于0的正整数 代码如下: <input onkeyup="i ...
- 从头开始构建LINUX [LFS]
“LINUX就是这个范”有一章专门介绍了Linux的构建,过程详细,很有意思.结合这方面的资料简要汇集一下 LFS 这个站点提供了从源代码构建一个Linux的详细步骤 书 http://archive ...
- NSCopy&NSMutableCopy
struct student { int a; float f; char c; long l; }; struct person { int a; float f; char c; long l; ...
- iOS RESideMenu 侧滑 第三方类库
下载地址:https://github.com/romaonthego/RESideMenu 效果如下:官方案例 自己的实现效果 具体代码下: AppDelegate.m文件中 - (BOOL)app ...
- python之选课系统详解[功能未完善]
作业需求 思路:1.先写出大体的类,比如学校类,学生类,课程类-- 2.写出类里面大概的方法,比如学校类里面有创建讲师.创建班级-- 3.根据下面写出大致的代码,并实现其功能 遇到的困 ...
- Jenkins 2.0 要来了
Jenkins 在2016/02/29日发布了2.0 alpha版本,https://jenkins-ci.org/2.0/ , 改进界面,向前兼容,增加新功能: 1.初始化时可以选择推荐插件或自定义 ...