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))。
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int n,sum[2005],c[2005],dp[2005][2005]; 5 int main() 6 { 7 scanf("%d",&n); 8 for(int i=n;i>=1;i--)scanf("%d",&c[i]); 9 for(int i=1;i<=n;i++)sum[i]=sum[i-1]+c[i]; 10 for(int i=1;i<=n;i++) 11 { 12 for(int j=1;j<=n;j++) 13 { 14 dp[i][j]=dp[i][j-1]; 15 int k=2*j-1; 16 if(k<=i)dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]); 17 k++; 18 if(k<=i)dp[i][j]=max(dp[i][j],sum[i]-dp[i-k][k]); 19 } 20 for(int j=1;j<=n;j++) 21 printf("%d ",dp[i][j]); 22 printf("\n"); 23 } 24 25 printf("%d\n",dp[n][1]); 26 return 0; 27 }
原文地址:https://www.cnblogs.com/taozi1115402474/p/11688278.html
- 接口测试 | 24 requests + unittest集成你的接口测试
- [快学Python3]数据结构与算法-二分查找
- 基于Excel参数化你的Selenium2测试
- 【LeetCode】关关刷题日记24-Leetcode 121. Best Time to Buy and Sell Stock
- 线性表的链式存储结构的实现及其应用(C/C++实现)
- [接口测试 - 基础篇] 01 你应该了解的协议基础
- 使用TensorFlow实现神经网络的介绍
- HTTP协议报文结构及抓包报文分析示例
- 必备 .NET - C# 异常处理
- Java Socket获取本机的InetAddress实例
- 机器理解大数据秘密:聚类算法深度剖析
- BZOJ 3668: [Noi2014]起床困难综合症【贪心】
- 用C#实现字符串相似度算法(编辑距离算法 Levenshtein Distance)
- 51 Nod 1007 正整数分组【类01背包】
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 无所不能的Embedding 2. FastText词向量&文本分类
- 5. Pandas系列 - 重建索引
- 6. Pandas系列 - 迭代
- Java 虚拟机基础原理:功能,内存管理,类的加载机制,分析字节码执行过程
- Stack有性能问题?推荐用ArrayDeque队列!队列是什么?什么是双端队列、延迟系列、阻塞队列,全是知识盲区!
- 纯CSS实现照片墙效果
- Tungsten Fabric知识库丨构建、安装与公有云部署
- 7. Pandas系列 - 排序和字符串处理
- JVM调优,程序员必须掌握的知识
- 5分钟Flink - 侧输出流(SideOutput)
- 数据库技术:MySQL 基础和 SQL 入门,单表、约束和事务
- ConcurrentHashMap的size方法是线程安全的吗?
- 数据科学家极力推荐核心计算工具-Numpy的前世今生(上)
- 数据库技术:MySQL 多表,外键约束,数据库设计,索引,视图,存储过程触发器,数据控制,数据备份与恢复
- WordPress 站点地址被恶意篡改的防护方案讨论