2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

Time Limit: 10 Sec  Memory Limit: 64 MB

Submit: 418  Solved: 206

[Submit][Status][Discuss]

Description

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枚金币,第i枚金币的价值是Ci。贝西和邦妮把金币排成一条直线,她们轮流取金币,看谁取到的钱最多。贝西先取。每次仅仅能取一枚金币,并且仅仅能选择取直线两头的金币,不能取走中间的金币。

当全部金币取完之后。游戏就结束了。

    贝西和邦妮都是很聪明的,她们会採用最好的办法让自己取到的金币最多。请帮助贝西计算一下。她能拿到多少钱?

Input

* Line 1: A single integer: N * Lines 2..N+1: Line i+1 contains a single integer: C_i

第一行:单个整数N,表示硬币的数量,1<=N≤5000
第二行到第N+1行:第i+l行有一个整数Ci,代表第i块硬币的价值,1≤Ci≤5000

Output

* Line 1: A single integer, which is the greatest total value Bessie can win if both cows play optimally.

  第一行:单个整数,表示假设两方都按最优策略玩游戏,先手能够拿到的最大价值

Sample Input

4

30

25

10

35


Sample Output

60


HINT

(贝西最好的取法是先取35,然后邦妮会取30。贝西再取25,邦妮最后取10)

Source

动态规划的空间优化,感觉方法还是不错的。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define maxn 5005
using namespace std;
int n,x;
int f[maxn],sum[maxn];
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read();
sum[0]=0;
F(i,1,n)
{
x=read();
sum[i]=sum[i-1]+x;
f[i]=x;
}
F(j,1,n-1) F(i,1,n-j) f[i]=sum[i+j]-sum[i-1]-min(f[i],f[i+1]);
printf("%d\n",f[1]);
}

bzoj2101【Usaco2010 Dec】Treasure Chest 藏宝箱的更多相关文章

  1. BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 327  Solved:  ...

  2. BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )

    dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 ...

  3. bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 592  Solved:  ...

  4. 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ...

  5. BZOJ——2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit:  ...

  6. BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(这是我写过最骚气的dp!)

    题目描述 贝西和邦妮找到了一个藏宝箱,里面都是金币! 但是身为两头牛,她们不能到商店里把金币换成好吃的东西,于是她们只能用这些金币来玩游戏了.   藏宝箱里一共有N枚金币,第i枚金币的价值是Ci.贝西 ...

  7. BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ...

  8. bzoj 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱【区间dp】

    就是区间dp啦f[i][j]表示以i开头的长为j+1的一段的答案,转移是f[i][j]=s[i+l]-s[i-1]+min(f[i][j-1],f[i+1][j-1]),初始是f[i][1]=a[i] ...

  9. [Usaco2010 Dec]Treasure Chest 藏宝箱

    题目链接:点这里 Solution: 刚开始以为是博弈论,然而不是... 首先考虑n方dp,设f(l,r)为只有\(l\)到\(r\)区间的钱的先手最大获利 那么我们可以得到式子f(l,r)=sum( ...

随机推荐

  1. SSM调用数据库存储过程

    ServiceImpl中: Map<String,Object> map=new HashMap<String,Object>(); map.put("bid&quo ...

  2. LeetCode(81) Search in Rotated Array II

    题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

  3. [MVC][Shopping]Copy Will's Code

    数据模型规划(Models) //DisplayNameAttribute 指定属性的显示名称 [DisplayName("商品类别")] //DisplayColumnAttri ...

  4. 大数据学习——实现多agent的串联,收集数据到HDFS中

    采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs,使用agent串联 根据需求,首先定义以下3大要素 第一台flume agent l  ...

  5. 大数据学习——JAVA采集程序

    1 需求 从外部购买数据,数据提供方会实时将数据推送到6台FTP服务器上,我方部署6台接口采集机来对接采集数据,并上传到HDFS中 提供商在FTP上生成数据的规则是以小时为单位建立文件夹(2016-0 ...

  6. zoj 2109 FatMouse' Trade

    FatMouse' Trade Time Limit: 2 Seconds      Memory Limit: 65536 KB FatMouse prepared M pounds of cat ...

  7. Codeforces Round #386 (Div. 2) A+B+C+D!

    A. Compote 水题(数据范围小都是水题),按照比例找最小的就行了,3min水过. int main() { int a,b,c; while(~scanf("%d%d%d" ...

  8. CodeForces - 592D Super M 题解

    题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...

  9. 【二分贪心+精度问题】F. Pie

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/F [题意] 给定n个已知半径的披萨,有m个人要分这n个披萨 要求每个人分到的面积 ...

  10. android L版本AAL新架构

    [DESCRIPTION] 和之前KK版本相比,在L版本上面AAL的架构也有发生一些改变. 拿掉了之前KK平台上使用的MTK LABC,使用Android原生的自动背光功能. AALService内部 ...