2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 327  Solved: 147
[Submit][Status]

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

题解:
看着这道题想起了红书上的取数游戏n为偶数,贪心算法,选择奇数位和偶数位里面和最大的即可保证先手必胜,
发现没法处理n为奇数的情况,然后yy了一个错误的算法。。。
因为取数游戏只需要保证先手必胜即可,而该题则是求出最大可能得分,赢!=最大得分。。。
代码:(错误)
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 5000+100
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,a[maxn],b[];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
n=read();
for1(i,n)a[i]=read();
for1(i,n)b[i&]+=a[i];
if(!(n&))printf("%d\n",max(b[],b[]));
else printf("%d\n",b[]+b[]-min(max(b[],b[]-a[n]),max(b[],b[]-a[])));
return ;
}

膜拜了题解之后发现此题竟然这么神!

首先O(N^2)的DP挺好想的.

f[i,j]=sum[i,j]-min(f[i+1,j],f[i,j-1]).

但题目把n出到5000,内存卡到64M,二维的状态存不下..

其实,j这一维可以省掉.我们换个状态表示

f[i,i+len]=sum[i,i+len]-min(f[i+1,i+len],f[i,i+len-1])

然后循环这样写:

for len=1 to n

for i=1 to n-len.

容易看出第二维可以省掉了.

orzzzzzzzzzzzzzzzzzz

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 5000+100
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,s[maxn],f[maxn];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)
{
f[i]=read();
s[i]=s[i-]+f[i];
}
for1(i,n-)
for2(j,,n-i)
f[j]=s[j+i]-s[j-]-min(f[j],f[j+]);
printf("%d\n",f[]);
return ;
}

BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. [Usaco2010 Dec]Treasure Chest 藏宝箱

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

  9. bzoj2101【Usaco2010 Dec】Treasure Chest 藏宝箱

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 418  Solved: ...

随机推荐

  1. DPDK2.1 linux上开发入门手册

    1引言 本文档主要包含INTEL DPDK安装和配置说明.目的是让用户快速的开发和运行程序.文档描述了如何在不深入细节的情况下在linux应用开发环境上编译和运行一个DPDK应用程序. 1.1文档总览 ...

  2. Hibernate Validation使用示例及讲解

    Hibernate Validation使用示例及讲解 时间 -- :: ITeye-博客 原文 http://wdmcygah.iteye.com/blog/2174680 主题 Java 在项目开 ...

  3. jTemplates——学习(1)

    这里介绍一个基于jQuery开发的模板引擎. jTemplates目前最新的版本是0.7.8,由tPython开发.官方网站:http://jtemplates.tpython.com 两个附件, 一 ...

  4. zeptoJS:如何像jQuery一样,让滚动变得优雅?

    利用jQuery的animate() 方法,我们很容易实现滚动条的平滑滚动效果: $(function() { $('#top').click( function (e) { $('html, bod ...

  5. 魅蓝Note2跑分 MT6753性能究竟如何

    MT6753实力究竟如何? 采用LP工艺的MT6753实际上在性能和功耗方面并不比MT6752高,相反,同频下功耗要高1/3左右.并且其内存带宽是5.3G/s,小于MT 6752的6.4G/s 而且没 ...

  6. Android Fragment 详解(一)

    Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...

  7. HDU 2159 FATE(全然背包+二维费用背包)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. LDAP索引及缓存优化

    一.设置索引 索引将查找信息和 Directory Server 条目关联起来. Directory Server支持以下几种索引: 1出现索引 (pres) - 列出了具有特定属性的条目,与属性的值 ...

  9. (转).NET平台开源JSON库LitJSON的使用方法

    一个简单示例: String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemi ...

  10. js基本类型

    1.undefined 1)var a;//没有赋值的时候就是undefined 2)undefined派生自null,alert(undefined==null)//true 虽然这上条语句是一样, ...