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

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

时间:2017-11-25 11:24:55      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:wal   sid   一个   sed   getchar   计算   walk   color   des   

http://www.lydsy.com/JudgeOnline/problem.php?id=2101

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 539  Solved: 294
[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

Silver

 

 

区间DP:f[l][r]表示区间[l,r]之间取硬币的最优结果,那么

如果,贝西取在l点,则邦妮去完f[l+1][r],f[l][r]=sum[r]-sum[l-1]-f[l+1][r],贝西取在r同理

 

技术分享图片
 1 #include <cstdio>
 2 
 3 #define max(a,b) (a>b?a:b)
 4 #define min(a,b) (a<b?a:b)
 5 
 6 inline void read(int &x)
 7 {
 8     x=0; register char ch=getchar();
 9     for(; ch>9||ch<0; ) ch=getchar();
10     for(; ch>=0&&ch<=9; ch=getchar()) x=x*10+ch-0;
11 }
12 
13 const int N(5233);
14 int f[N][N],sum[N];
15 int n,w[5005];
16 
17 int Presist()
18 {
19     read(n);
20     for(int i=1; i<=n; ++i)
21     {
22         read(w[i]);
23         f[i][i]=w[i];
24         sum[i]=sum[i-1]+w[i];
25     }
26     for(int L=2; L<=n; ++L)
27         for(int r,l=1; l<=n-L+1; ++l)
28         {
29             r=l+L-1;
30             f[l][r]=max(f[l][r],sum[r]-sum[l-1]-
31                         min(f[l+1][r],f[l][r-1]));
32         }
33     printf("%d\n",f[1][n]);
34     return 0;
35 }
36 
37 int Aptal=Presist();
38 int main(int arg,char**argv){;}
二维

 

该题需要压维、

 

 1 #include <cstdio>
 2 
 3 #define max(a,b) (a>b?a:b)
 4 #define min(a,b) (a<b?a:b)
 5 
 6 inline void read(int &x)
 7 {
 8     x=0; register char ch=getchar();
 9     for(; ch>9||ch<0; ) ch=getchar();
10     for(; ch>=0&&ch<=9; ch=getchar()) x=x*10+ch-0;
11 }
12 
13 const int N(5233);
14 int n,f[N],sum[N];
15 
16 int Presist()
17 {
18     read(n);
19     for(int w,i=1; i<=n; ++i)
20     {
21         read(w), f[i]=w;
22         sum[i]=sum[i-1]+w;
23     }
24     for(int L=1; L<n; ++L)
25       for(int r,l=1; l<=n-L; ++l)
26         f[l]=sum[l+L]-sum[l-1]-min(f[l+1],f[l]);
27     printf("%d\n",f[1]);
28     return 0;
29 }
30 
31 int Aptal=Presist();
32 int main(int arg,char**argv){;}

 

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

标签:wal   sid   一个   sed   getchar   计算   walk   color   des   

原文地址:http://www.cnblogs.com/Shy-key/p/7894391.html

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