码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

时间:2014-10-01 14:28:21      阅读:576      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

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了一个错误的算法。。。
因为取数游戏只需要保证先手必胜即可,而该题则是求出最大可能得分,赢!=最大得分。。。
代码:(错误)
bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 5000+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
28     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}
29     return x*f;
30 }
31 int n,a[maxn],b[2];
32 int main()
33 {
34     //freopen("input.txt","r",stdin);
35     //freopen("output.txt","w",stdout);
36     n=read();
37     for1(i,n)a[i]=read();
38     for1(i,n)b[i&1]+=a[i];
39     if(!(n&1))printf("%d\n",max(b[0],b[1]));
40     else printf("%d\n",b[0]+b[1]-min(max(b[0],b[1]-a[n]),max(b[0],b[1]-a[1])));
41     return 0;
42 }
View Code

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

首先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

代码:

bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 5000+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
28     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}
29     return x*f;
30 }
31 int n,s[maxn],f[maxn];
32 int main()
33 {
34     freopen("input.txt","r",stdin);
35     freopen("output.txt","w",stdout);
36     n=read();
37     for1(i,n)
38     {
39         f[i]=read();
40         s[i]=s[i-1]+f[i];
41     }
42     for1(i,n-1)
43      for2(j,1,n-i)
44       f[j]=s[j+i]-s[j-1]-min(f[j],f[j+1]);
45     printf("%d\n",f[1]);
46     return 0;
47 }
View Code

 

BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/zyfzyf/p/4003188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!