bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 592 Solved: 319
[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.
Input
* Line 1: A single integer: N * Lines 2..N+1: Line i+1 contains a single integer: C_i
Output
* Line 1: A single integer, which is the greatest total value Bessie can win if both cows play optimally.
Sample Input
30
25
10
35
Sample Output
HINT
(贝西最好的取法是先取35,然后邦妮会取30,贝西再取25,邦妮最后取10)
Source
/*
设f[i][j]为[i,j]这段区间先手能获得最大值
初值f[i][i]=a[i],因此要从小区间推大区间。
显然先手要让对手得到的最小,用区间的和减去对手的最小值就是先手的最大值
以为先手操作一次后就成了另一个人先手
所以f[i][j]=sum[j]-sum[i-1]-min(f[i+1][j],f[i][j+1])
炸空间。
可以看出f[][j]只会被f[][j]或者f[][j+1]更新,可以滚动数组压掉一维。
f[0/1][i]表示以i为起点,某段长度的最大值。
转移时先枚举区间长度,f[k^1][i]=sum[j]-sum[i-1]-min(f[k][i],f[k][i+1]);
因为区间长度从小到大枚举,所以枚举到这个长度时右端点为i+len-1,上个长度是i+len-2
所以dp方程里f[k][i]储存的是上个区间长度即f[l][r-1],f[k][i+1]储存的是f[l+1][r])
*/
#include<bits/stdc++.h> #define N 5001 using namespace std;
int n,m,k,ans,cnt;
int f[][N],sum[N]; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int main()
{
freopen("ly.in","r",stdin);
n=read();
for(int i=;i<=n;i++)
{
sum[i]=read();
f[k][i]=sum[i];sum[i]+=sum[i-];
}
for(int L=;L<=n;L++)
{
for(int i=;i+L-<=n;i++)
{
int j=i+L-;
f[k^][i]=sum[j]-sum[i-]-min(f[k][i],f[k][i+]);
}k^=;
}
printf("%d\n",f[k][]);
return ;
}
bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)的更多相关文章
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 327 Solved: ...
- BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )
dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 ...
- 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ...
- BZOJ——2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
http://www.lydsy.com/JudgeOnline/problem.php?id=2101 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: ...
- BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(这是我写过最骚气的dp!)
题目描述 贝西和邦妮找到了一个藏宝箱,里面都是金币! 但是身为两头牛,她们不能到商店里把金币换成好吃的东西,于是她们只能用这些金币来玩游戏了. 藏宝箱里一共有N枚金币,第i枚金币的价值是Ci.贝西 ...
- [Usaco2010 Dec]Treasure Chest 藏宝箱
题目链接:点这里 Solution: 刚开始以为是博弈论,然而不是... 首先考虑n方dp,设f(l,r)为只有\(l\)到\(r\)区间的钱的先手最大获利 那么我们可以得到式子f(l,r)=sum( ...
- BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ...
- 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] ...
- [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp
Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...
随机推荐
- 最详细的JavaWeb开发基础之java环境搭建(Mac版)
阅读文本大概需要 5 分钟. 我之前分享过在 Windows 下面配置 Java 环境,这次给大家带来的是 Mac 下面安装配置 Java 环境.首先 Mac 系统已经带有默认的 Java,但是由于使 ...
- 【03】AJAX 向服务器发送请求
AJAX 向服务器发送请求 创建 XMLHttpRequest 对象后,就可以向服务器发送请求了. XMLHttpRequest 对象的 open() 方法和 send() 方法用来向服务器发送请 ...
- jsp学习之如何在web层创建Servlet
jsp动态网页的文件目录结构如下: 1.src下存放java代码 2.包web_xx为web层 3.webcontent里面是jsp文件 jsp页面中的请求交付给servlet处理,在实际中 jsp的 ...
- [luoguP1058] 立体图(超级大模拟(¬︿̫̿¬☆))
传送门 看到题后整个人成了mengbier 但是仔细分析一下就很简单了,先确定好输出的图的长和宽. 然后从输入的矩形的左上角的最下面的开始填充,顺序是从下到上,从左到右,从后往前. 填充的时候直接覆盖 ...
- HDU 4968 (水dp 其他?)
+;],dp1[][],dp2[][]; map< memset(GPA,, ;i<=;i++) hash[i]=; ;i<=;i++) hash[i]=; ...
- Memory Ordering in Modern Microprocessors
Linux has supported a large number of SMP systems based on a variety of CPUs since the 2.0 kernel. L ...
- Subsets and Subsets II (回溯,DFS,组合问题)
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- Codeforces 799E(贪心)
题意: 有n个物品,购买物品i需要花费ci的代价.Arkady和Masha分别有喜欢的物品. 现在需要从中选m个,使得这m个物品中至少有k个Arkady喜欢的物品,k个Masha喜欢的物品. 输出满足 ...
- canvas 插件
http://www.jq22.com/yanshi2217 参考这个站 发现一些比较有用的canvas插件: 线形图插件:jquery.sparkline 2.1.1 excanvas 环形图,饼状 ...
- [教程] NETGEAR R7800 路由器TFTP刷机方法(适用于.img格式固件各种刷)
本教程是我参照R7800的OP/LEDE固件交流群内文件做的教程,可以说是完善.补充吧. 本帖适用于:① 原厂固件刷原厂固件:② 原厂固件刷第三方固件(.img格式):③ 第三方固件刷回原厂固件(.i ...