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

dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) )

被卡空间....我们可以发现 l > r 是无意义的 , 所以可以省下一半的空间

--------------------------------------------------------------------------------

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<iostream>

#define rep( i , n ) for( int i = 0 ; i < n ; ++i )

#define clr( x , c ) memset( x , c , sizeof( x ) )

#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )

using namespace std;

const int maxn = 5000 + 10;

int d[ maxn * maxn >> 1 ];

int sum[ maxn ];

int n;

#define f( l , r ) ( r + ( ( ( n << 1 ) - l + 2 )  * ( l - 1 ) >> 1 ) )

int dp( int l , int r ) {

int &ans = d[ f( l , r ) ];

if( ans != -1 ) return ans;

return ans = sum[ r ] - sum[ l - 1 ] - min( dp( l + 1 , r ) , dp( l , r - 1 ) );

}

int main() {

freopen( "test.in" , "r" , stdin );

cin >> n;

sum[ 0 ] = 0;

clr( d , -1 );

Rep( i , n ) {

int v;

scanf( "%d" , &v );

d[ f( i , i ) ] = v;

sum[ i ] = sum[ i - 1 ] + v;

}

cout << dp( 1 , n ) << "\n";

return 0;

}

--------------------------------------------------------------------------------

2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

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

时间: 2024-11-03 22:43:14

BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )的相关文章

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱*

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱 题意: 给个序列,A与B轮流取数,谁取的数总和大谁赢.每次只能取序列两端,问A能取的数总和最大是多少.假设两人都用最优策略.序列大小≤5000 题解: dp.f[i][j][0]=max(f[i+1][j][1]+a[i],f[i][j-1][1]+a[j]),f[i][j][1]=min(f[i+1][j][0],f[i][j-1][0]). 代码: 1 #include <cstdio> 2 #includ

bzoj 2097: [Usaco2010 Dec]Exercise 奶牛健美操【二分+树形dp】

二分答案,然后dp判断是否合法 具体方法是设f[u]为u点到其子树中的最长链,每次把所有儿子的f值取出来排序,如果某两条能组合出大于mid的链就断掉f较大的一条 a是全局数组!!所以要先dfs完子树才能填a!! #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1000005; int n,m,h

BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

有点类似背包 , 就是那样子搞... ------------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ;  i < n ; ++i

BZOJ 2100: [Usaco2010 Dec]Apple Delivery

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2100 解: 典型的最短路,从两个点都跑一遍模板就好了,不过有点麻烦的是,如果跑dijkstra,要用堆来优化,如果跑spfa要用SLF(当然习惯用LLL也行)来优化,总之不能裸的模板去跑. 程序: #include<iostream> #include<cstdio> #include<cstring> #include<queue> #define

一道看似dp实则暴力的题 Zombie&#39;s Treasure Chest

 Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是dp中的背包问题,但是由于数据量太大,用dp肯定会超时,所以只能寻找另外一种思路,可以用贪心加暴力,先求出两种宝石大小的最小公倍数com,然后将N/com-com,与N%comkanchengs看成是两个部分(想想应该明白).将前一个部分,放入单位价值量最高的那个,对于后面那个部分直接将S1的数量从

cug oj 1479 Treasure Chest Lock (区间dp 思维)

1479: Treasure Chest Lock Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 7  Solved: 5 [Submit][Status][Web Board] Description Vic has a treasure chest. And there is a lock on the treasure chest. The lock contains a sequence of wheels. Each wheel has

[Usaco2010 Dec]Exercise 奶牛健美操

[Usaco2010 Dec]Exercise 奶牛健美操 题目 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径.简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1. 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径.如果直径太大,奶牛们就会拒绝锻炼. Farmer John把每个点标记为1..V

洛谷P3004 [USACO10DEC]宝箱Treasure Chest

P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 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

洛谷 P3004 [USACO10DEC]宝箱Treasure Chest

P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 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