洛谷 P3004 [USACO10DEC]宝箱Treasure Chest
Bessie and Bonnie have found a treasure chest full of marvelous gold coins! Being cows, though, they can't just walk into a store and buy stuff, so instead they decide to have some fun with the coins.
The N (1 <= N <= 5,000) coins, each with some value C_i (1 <= C_i <= 5,000) are placed in a straight line. Bessie and Bonnie take turns, and for each cow's turn, she takes exactly one coin off of either the left end or the right end of the line. The game ends when there are no coins left.
Bessie and Bonnie are each trying to get as much wealth as possible for themselves. Bessie goes first. Help her figure out the maximum value she can win, assuming that both cows play optimally.
Consider a game in which four coins are lined up with these values:
30 25 10 35
Consider this game sequence:
Bessie Bonnie New Coin
Player Side CoinValue Total Total Line
Bessie Right 35 35 0 30 25 10
Bonnie Left 30 35 30 25 10
Bessie Left 25 60 30 10
Bonnie Right 10 60 40 --
This is the best game Bessie can play.
贝西和伯尼找到了一个装满了金币的宝箱!但是,作为奶牛,他们不能随便进入一家商店去买东西。所以他们决定去用这些金币玩一个游戏。
这里有N(1<=N<=5000)个硬币,每个都有一个价值C_i(1<=C_i<=5000)。这些硬币被摆成了一行。贝西和伯尼每人一回合。到了一只奶牛的回合时,他就要拿走最左边或者最右边的硬币。当没有硬币时,游戏结束。
贝西和伯尼都想要使自己拿到的金币价值尽量高,贝西先拿。现在贝西想要你帮帮她,算出她最多可以拿多少钱(伯尼也会尽量取到最优)。
输入输出格式
输入格式:
Line 1: A single integer: N
- Lines 2..N+1: Line i+1 contains a single integer: C_i
输出格式:
- Line 1: A single integer, which is the greatest total value Bessie can win if both cows play optimally.
输入输出样例
4
30
25
10
35
60
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,w[],s[],f[][];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&w[i]);
s[i]=s[i-]+w[i];
f[i][i]=w[i];
}
for(int i=n;i>=;i--)
for(int j=i+;j<=n;j++)
f[i][j]=max((s[j]-s[i-])-f[i+][j],(s[j]-s[i-])-f[i][j-]);
cout<<f[][n]<<" "<<s[n]-f[][n];
}
洛谷 P3004 [USACO10DEC]宝箱Treasure Chest的更多相关文章
- 洛谷P3004 [USACO10DEC]宝箱Treasure Chest
P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...
- 洛谷3004 [USACO10DEC]宝箱Treasure Chest
题目:https://www.luogu.org/problemnew/show/P3004 一眼看上去就是记忆化搜索的dp.像 一双木棋 一样. 结果忘了记忆化.T了5个点. 然后加上记忆化.MLE ...
- [LUOGU] P3004 [USACO10DEC]宝箱Treasure Chest
第一眼:区间DP,可以瞎搞 f[i][j]=max(sum(i,j)-f[i+1][j],sum(i,j)-f[i][j-1]) 提出来就是f[i][j]=sum(i,j)-min(f[i+1][j] ...
- [USACO10DEC]宝箱Treasure Chest
区间DP,但是卡空间. n2的就是f[i,j]=sum[i,j]-min(f[i+1][j],f[i][j-1])表示这个区间和减去对手取走的最多的. 但是空间是64MB,就很难受 发现一定是由大区间 ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
- 洛谷P3004 宝箱Treasure Chest——DP
题目:https://www.luogu.org/problemnew/show/P3004 似乎有点博弈的意思,但其实是DP: f[i][j] 表示 i~j 的最优结果,就可以进行转移: 注意两个循 ...
- 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...
- 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...
- G - Zombie’s Treasure Chest(动态规划专项)
G - Zombie’s Treasure Chest Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
随机推荐
- 洛谷 P1081 开车旅行 —— 倍增
题目:https://www.luogu.org/problemnew/show/P1081 真是倍增好题! 预处理:f[i][j] 表示从 i 点开始走 2^j 次 AB (A,B各走一次)到达的点 ...
- succ
- 希尔shell排序——java实现
希尔排序是对插入排序的优化,将插入排序的交换步长由1增加到h. 希尔排序的思想是使数组中任意间隔为h的元素有序.步长调幅为h = 3*h + 1, 也就是1,4,13,40,121,364, 1003 ...
- css定位、position与float同时使用的情况
一.css定位 CSS 有三种基本的定位机制:普通流.浮动和绝对定位. 1.普通流:未专门指定的元素都在普通流中定位,position:static/relative;和float:none;也在普通 ...
- 0502 php简单了解
准备工作: 安装好wamp,配置站点:apache2.4.9\conf\httpd.conf 注意事项: 1.必须有分号 2.不要有无意义空行,会以空格形式输出. 3.变量,关键字(if,for..) ...
- preg_match_all匹配网络上文件
<?php$ssa=file_get_contents("http://www.oschina.net/code/snippet_4873_5256");preg_match ...
- 《疯狂Python讲义》重要笔记——Python简介
简介 Python是一种面向对象.解释型.弱类型的脚本语言,同时也是一种功能强大的通过语言,它提供了高效的高级数据结构,还有简单有效的面向对象编程. 在大数据.人工智能(AI)领域应用广泛,因此变得流 ...
- SQL之LEFT JOIN,EIGHT JOIN,INSERT JOIN的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- JAVA 中进行网络通信时,通信的程序两端要传输的对象,不仅要序列化,而且这个对象所属的类的名字要完全一样,连包的名字都得一样
如上图项目目录,这是一个简易的QQ,客户端登录的时候要传输用户信息到服务器验证,所以两端都会用到User类的对象,但一开始我在Server端的包名是com.qq.server.common,两端的报名 ...
- 改善用户体验 Web前端优化策略总结
前端是庞大的,包括HTML.CSS.Javascript.Image.Flash等等各种各样的资源.前端优化是复杂的,针对方方面面的资源都有不同的方式.那么,前端优化的目的是什么? 1. 从用户角度而 ...