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)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
随机推荐
- tensorboard在Mac OS X系统环境下如何启动
再次必须写一篇博客,一次来说明这打开tensorboard的艰难之路,遇到了好多错误,真的是走了好多弯路,最后还是解决了 一开始总是报错,不知道是为什么,其实还是自己没有看懂原理,就冲动的开始招呼画瓢 ...
- 【BZOJ3944】Sum
题面 Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1, ...
- Nginx 和 Gunicorn 部署 Django项目
目录 Nginx 和 Gunicorn 部署 Django项目 配置Nginx 安装配置Gunicorn 通过命令行直接启动 Gunicorn 与 uwsgi 的区别,用哪个好呢 Gunicorn u ...
- Clion IDE的安装
/下载压缩包 wget https://download.jetbrains.8686c.com/cpp/CLion-2016.2.2.tar.gz //解压 tar -zxvf CLion-2016 ...
- KNN算法和实现
KNN要用到欧氏距离 KNN下面的缺点很容易使分类出错(比如下面黑色的点) 下面是KNN算法的三个例子demo, 第一个例子是根据算法原理实现 import matplotlib.pyplot as ...
- sql.xml where ids in的写法
<if test="iSurfaceTypeArray != null"> AND b.i_SurfaceType in <!-- 根据外观检查查询 --> ...
- 数据库DSN是什么
数据库建立好之后,要设定系统的 DSN(数据来源名称),才能让网页可以知道数据库所在的位置以及数据库相关的属性.使用DSN的好处还有,如果移动数据库档案的位置,或是换成别种类型的数据库,只要重新设定 ...
- 2019牛客暑期多校赛(第三场)B-求01串中的最长01数量相等的子串和子序列
https://ac.nowcoder.com/acm/contest/883/B 首先先把0所在的位置变-1,1所在位置变1,然后统计一个前缀和,用sum[i]表示. 那么如果从起点开始的话只要满足 ...
- 左神算法进阶班5_4设计可以变更的缓存结构(LRU)
[题目] 设计一种缓存结构,该结构在构造时确定大小,假设大小为K,并有两个功能: set(key, value):将记录(key, value)插入该结构. get(key):返回key对应的valu ...
- 数据库备份还原——mysqlbackup与mysqldump对比测试
1 环境描述 1.1 硬件环境 服务器类型:华为RH5885 IP: 10.148.128.100 内存: 64G 物理CPU个数:4 CPU核数:8 逻辑CPU个数:64 Int ...