TZOJ 2965 A Coin Game(DP)
描述
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 Ci (1 ≤ Ci ≤ 100,000).
The first player starts the game by taking the top one or two coins (C1 and maybe C2) 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?
输入
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer: Ci
输出
* Line 1: A single integer representing the maximum value that can be made by the first player.
样例输入
5
1
3
1
7
2
样例输出
9
题意
有两个人n枚硬币,A先手可以取1-2个,B最多可以取A*2个,问A的最大总价值。
题解
dp[i][j]表示剩下1-i,上个人取了j枚的最大总价值。
那么答案显然是dp[n][1],表示剩下1-n,上个人取了1枚(虚的),那么先手就可以取1枚或者2枚。
O(n^3)的转移dp[i][j]=max(sum[i]-dp[i-k][k])(1<=k<=2*j)。
易得dp[i][j-1]=max(sum[i]-dp[i-k][k])(1<=k<=2*j-2)。
两个相差sum[i]-dp[i-2*j][2*j]和sum[i]-dp[i-(2*j-1)][2*j-1]。
所以O(n^3)的转移可以优化一层变成O(n^2)。
dp[i][j]=(dp[i][j-1]或者max(sum[i]-dp[i-k][k])(2*j-1<=k<=2*j))。
代码
#include<bits/stdc++.h>
using namespace std; int n,sum[],c[],dp[][];
int main()
{
scanf("%d",&n);
for(int i=n;i>=;i--)scanf("%d",&c[i]);
for(int i=;i<=n;i++)sum[i]=sum[i-]+c[i];
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
dp[i][j]=dp[i][j-];
int k=*j-;
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]);
}
for(int j=;j<=n;j++)
printf("%d ",dp[i][j]);
printf("\n");
} printf("%d\n",dp[n][]);
return ;
}
TZOJ 2965 A Coin Game(DP)的更多相关文章
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- 【题解】284E. Coin Troubles(dp+图论建模)
[题解]284E. Coin Troubles(dp+图论建模) 题意就是要你跑一个完全背包,但是要求背包的方案中有个数相对大小的限制 考虑一个\(c_i<c_j\)的限制,就是一个\(c_i\ ...
- [HDOJ]Coin Change(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2069 题意 有面值1,5,10,25,50的硬币数枚,对于输入的面值n,输出可凑成面值n(且限制总硬笔 ...
- UVA 10328 - Coin Toss dp+大数
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [HRBUST1472]Coin(dp,计数)
题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1472 题意:给n个硬币,面值随意.问恰好凑成m元的种类数(去掉重复). dp(i,j,k ...
- TZOJ 5101 A Game(区间DP)
描述 Consider the following two-player game played with a sequence of N positive integers (2 <= N & ...
- BZOJ2017[USACO 2009 Nov Silver 1.A Coin Game]——DP+博弈论
题目描述 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 <= N <= 2,000)枚硬币的堆栈放在地上,从堆顶数起的第I枚硬币的 ...
- TZOJ 3295 括号序列(区间DP)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
随机推荐
- sql 字符串连接
const string FMCG_BASH = "清除重复商品"; var sqls = new List<string>(); //// Fmcg sqls.},R ...
- Ubuntu 18.04/18.10快速开启Google BBR的方法
说明:Ubuntu 18.04改变挺大的,内核直接升到了正式版4.15,而BBR内核要求为4.9,也就是说满足了,所以我们不需要换内核就可以很快的开启BBR,这里简单说下方法. 提示:Ubuntu 1 ...
- springcloud Finchley 版本hystrix 和 hystrix Dashboard
hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...
- 网页qq在线交谈
网页中如何启用QQ交谈 1. 登录QQ, 打开网址:http://shang.qq.com/v3/widget.html 启用QQ通讯组件. 2. 选择组件样式,设置提示语,例如: 3. 刷新页面,C ...
- h5页面在不同ios设备上的问题总结
1.日期问题 对于yyyy-mm-dd hh:mm:ss 这种格式在ios系统不识别 时间格式化的时候,在浏览器端处理好好的,到了手机端,就变成NAN,或者null,这种情况,是ios系统不能转化这种 ...
- bzoj2209 括号序列
题意:给你一个括号序列.操作1:询问需要更改多少个括号使之匹配. 操作2:反转序列,左括号变成右括号. 操作3:翻转序列,倒置. 标程: #include<cstdio> #include ...
- 你必须知道的.NET Day1
- C++ Builder获取系统文件的路径
取得路径的程序:(注意红色字体,由于博客显示问题,所以中间加了空格,大家自己把空格去掉即可) // -------------------------------------------------- ...
- React的PropTYpes
React的PropTYpes和获取真实DOM 组件的属性可以接受任意值,字符串,对象,函数等等都可以.有时,我们需要一种机制,验证别人使用组件时,提供的参数是否符合要求. 组件类的PropsType ...
- H-ui 前端框架
H-ui 前端框架 架起设计与后端的桥梁轻量级前端框架,简单免费,兼容性好,服务中国网站. 首个付费版产品 H-ui.admin.Pro,盘他!