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)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
随机推荐
- Java 高级面试知识点汇总!
1.常用设计模式 单例模式:懒汉式.饿汉式.双重校验锁.静态加载,内部类加载.枚举类加载.保证一个类仅有一个实例,并提供一个访问它的全局访问点. 代理模式:动态代理和静态代理,什么时候使用动态代理. ...
- MFC 窗口刷新防止闪烁方法
防止窗口闪烁的方法 1.将Invalidate()替换为InvalidateRect(). Invalidate()会导致整个窗口的图象重画,需要的时间比较长,而InvalidateRect()仅仅重 ...
- springAop的使用
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...
- opencv-图像遍历
#include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...
- 制作windows10系统启动U盘,从零开始。
1.打开百度,搜索windows下载,选个这个点击进去. 2.会看到下图,然后点击立即下载工具按钮. 3.接下来由于网络的原因,可能需要漫长的等待.会下载一个MediaCreationTool1903 ...
- html-圣杯布局
1.两边固定 当中自适应 2.当中列要完整显示 3.当中列要优先加载 浮动: 搭建完整的布局框架 margin 为赋值:调整旁边两列的位置(使三列布局到一行上) 使用相对定位:调整旁边两列的位置(使两 ...
- BZOJ 3668: [Noi2014]起床困难综合症
Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2693 Solved: 1563 [Submit][Status][Discuss] Descript ...
- DataGridView绑定DataTable的正确姿势
1. 将DataTable 绑定到BindingSource 2. 将BindingSource绑定到DataGridView 3. DataGridView修改完要从Datatable取值时,同步过 ...
- JDK源码阅读--AbstractStringBuilder
abstract class AbstractStringBuilder implements Appendable, CharSequence /** * The value is used for ...
- centos 以太坊多节点私链搭建
环境 centos 7 搭建 3 个节点的 私链. 第一步 安装 一些依赖的 工具 yum update -y && yum install git wget bzip2 vim ...