洛谷 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 ...
随机推荐
- 快速排序及三向切分快排——java实现
快速排序也是一种分治算法.主要思想是选取一个切分点,将大于切分点的元素都放置到数组右侧,小于切分点的元素都放置到数组左侧:然后递归,再对切分点左侧和右侧分别排序. 归并排序时递归在前,归并在后,快速排 ...
- myeclipse视图布局恢复
使用Windows 菜单下的 Reset Perspective
- ECharts-热力图实例
1.引入echarts.js 2.页面js代码 //用ajax获取所需要的json数据 $.get("../../../mall/queryPageWtSrPost.do", { ...
- HTML 14 JS事件
一 :什么是事件 发生的某一件事:触发特定的条件,完成某一项功能 二:学习的目的 在特定的条件下,完成特定的功能 条件满足的情况下,系统会自动执行 ( 回调 ) 绑定的方法 学习要点: 1.事件的两种 ...
- TCP排查常用命令
1.查看TCP连接状态命令 netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t" ...
- NPOI导出Excel自动计算公式问题
以前用过sheet.ForceFormulaRecalculation = true;当时能够自动计算出来. 今天把模板改了一下(没动公式,但是模板是老板改的,我也不知道他操作了什么),结果就不能自动 ...
- idea使用maven搭建ssm框架实现登陆商品增删改查
创建项目->maven->webapp->输入坐标->完成. pom.xml <project xmlns="http://maven.apache.org/P ...
- 【java基础】(3)Java继承内存分配
继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子类中定义的成员变量和父类中定义 ...
- JDBC: 批量处理提高SQL处理速度
引用:忘了 当需要成批插入或者更新记录时.可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率 JDBC的批量处理语句包括下面两个方法: a ...
- YOLO (You Only Look Once)
YOLO (You Only Look Once) dl cnn object detection 一.YOLO YOLO是一个实时的目标检测系统.最新的V2版本在Titan X 上可以每秒处理 ...