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.

输入输出样例

输入样例#1:

4
30
25
10
35
输出样例#1:

60
 思路:典型的区间DP

#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的更多相关文章

  1. 洛谷P3004 [USACO10DEC]宝箱Treasure Chest

    P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...

  2. 洛谷3004 [USACO10DEC]宝箱Treasure Chest

    题目:https://www.luogu.org/problemnew/show/P3004 一眼看上去就是记忆化搜索的dp.像 一双木棋 一样. 结果忘了记忆化.T了5个点. 然后加上记忆化.MLE ...

  3. [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] ...

  4. [USACO10DEC]宝箱Treasure Chest

    区间DP,但是卡空间. n2的就是f[i,j]=sum[i,j]-min(f[i+1][j],f[i][j-1])表示这个区间和减去对手取走的最多的. 但是空间是64MB,就很难受 发现一定是由大区间 ...

  5. 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...

  6. 洛谷P3004 宝箱Treasure Chest——DP

    题目:https://www.luogu.org/problemnew/show/P3004 似乎有点博弈的意思,但其实是DP: f[i][j] 表示 i~j 的最优结果,就可以进行转移: 注意两个循 ...

  7. 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...

  8. 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...

  9. G - Zombie’s Treasure Chest(动态规划专项)

    G - Zombie’s Treasure Chest Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

随机推荐

  1. 深入浅出时序数据库之预处理篇——批处理和流处理,用户可定制,但目前流行influxdb没有做

    时序数据是一个写多读少的场景,对时序数据库以及数据存储方面做了论述,数据查询和聚合运算同样是时序数据库必不可少的功能之一.如何支持在秒级对上亿数据的查询分组聚合运算成为了时序数据库产品必须要面对的挑战 ...

  2. 【POJ 3460】 Booksort

    [题目链接] http://poj.org/problem?id=3460 [算法] IDA* 注意特判答案为0的情况 [代码] #include <algorithm> #include ...

  3. CentOS/ubuntu iscsi initior target

    http://docs.oracle.com/cd/E26926_01/html/E25884/fpjwy.html iscsiadm: initiator reported error (24 - ...

  4. E20170826-hm

    squash   vt. 挤进; 将(某人[某物])压扁; 使沉默; 平定(叛乱等); meld vt. (使) 融合,合并,结合; n. 混合,合并; amend  vt. 修订; 改良,修改; a ...

  5. (三)Appium-desktop 打包

    appium-desktop经过二次开发后,需要打包为应用提供给其它同学使用.我们知道appium-desktop是使用electron来构建跨平台桌面应用程序.electron有electron-p ...

  6. 关于jquery的clone()和javascript的cloneNode()

    区别: jquery的clone( ),如果是true就是深克隆,把事件都会克隆过去:如果是false,则仅仅克隆的是结构: javascript的cloneNode( ),如果是true,会将子节点 ...

  7. VC常用代码之创建进程

    作者:朱金灿 来源:http://blog.csdn.net/clever101 创建进程是编程开发的常用操作.Windows中的创建进程采用API函数CreateProcess实现.下面是一个使用例 ...

  8. ListView中动态显示隐藏HeaderView和FooterView

    ListView中动态显示和隐藏Header&Footer 解决思路: 直接设置HeaderView和FooterView.setVisibility(View.GONE)无效, 布局仍然存在 ...

  9. php全局变量 超全局变量

    php中有许多超全局变量,这意味着它们在一个脚本的全部作用域中都可用.在函数或方法中无需执行 global $variable; 就可以访问它们. 这些超全局变量是: $GLOBALS    引用全局 ...

  10. Hibernate_01_初体验

    hibernate开发的基本步骤: 编写配置文档hibernate.cfg.xml: 编写实体类: 生成对应实体类的映射文件并添加到配置文档中: 调用hibernate API进行测试. Hibern ...