洛谷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 <= 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(1<=N<=5000)个硬币,每个都有一个价值C_i(1<=C_i<=5000)。这些硬币被摆成了一行。贝西和伯尼每人一回合。到了一只奶牛的回合时,他就要拿走最左边或者最右边的硬币。当没有硬币时,游戏结束。
贝西和伯尼都想要使自己拿到的金币价值尽量高,贝西先拿。现在贝西想要你帮帮她,算出她最多可以拿多少钱(伯尼也会尽量取到最优)。
输入输出格式
输入格式:
Line 1: A single integer: N
- Lines 2..N+1: Line i+1 contains a single integer: C_i
输出格式:
- Line 1: A single integer, which is the greatest total value Bessie can win if both cows play optimally.
输入输出样例
4
30
25
10
35
60
/*
区间dp
f[i][j]表示在i,j这段区间内先手能获得的最大分数;
那么后手在先手最优方案走法下,按最优方案走的最大分数就是
i,j这个区间总分数减去f[i][j].
*/
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 5010
int w[maxn],sum[maxn],f[maxn][maxn],n;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&w[i]);
sum[i]=sum[i-]+w[i];
f[i][i]=w[i];
}
for(int len=;len<=n;len++){
for(int l=;l+len-<=n;l++){
int r=l+len-;
f[l][r]=sum[r]-sum[l-]-min(f[l][r-],f[l+][r]);
}
}
printf("%d",f[][n]);
}
二维dp
#include<iostream>
#include<cstdio>
#define maxn 5010
using namespace std;
int n,sum[maxn],f[maxn];
int main(){
scanf("%d",&n);
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
sum[i]=sum[i-]+x;
f[i]=x;
}
for(int len=;len<=n;len++)
for(int l=;l+len-<=n;l++){
int r=l+len-;
f[l]=sum[r]-sum[l-]-min(f[l+],f[l]);
}
printf("%d",f[]);
}
一维dp
洛谷P3004 [USACO10DEC]宝箱Treasure Chest的更多相关文章
- 洛谷 P3004 [USACO10DEC]宝箱Treasure Chest
P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...
- 洛谷3004 [USACO10DEC]宝箱Treasure Chest
题目:https://www.luogu.org/problemnew/show/P3004 一眼看上去就是记忆化搜索的dp.像 一双木棋 一样. 结果忘了记忆化.T了5个点. 然后加上记忆化.MLE ...
- [LUOGU] P3004 [USACO10DEC]宝箱Treasure Chest
第一眼:区间DP,可以瞎搞 f[i][j]=max(sum(i,j)-f[i+1][j],sum(i,j)-f[i][j-1]) 提出来就是f[i][j]=sum(i,j)-min(f[i+1][j] ...
- [USACO10DEC]宝箱Treasure Chest
区间DP,但是卡空间. n2的就是f[i,j]=sum[i,j]-min(f[i+1][j],f[i][j-1])表示这个区间和减去对手取走的最多的. 但是空间是64MB,就很难受 发现一定是由大区间 ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
- 洛谷P3004 宝箱Treasure Chest——DP
题目:https://www.luogu.org/problemnew/show/P3004 似乎有点博弈的意思,但其实是DP: f[i][j] 表示 i~j 的最优结果,就可以进行转移: 注意两个循 ...
- 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...
- 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...
- G - Zombie’s Treasure Chest(动态规划专项)
G - Zombie’s Treasure Chest Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
随机推荐
- Java中synchronized
原文地址 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种:1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用 ...
- PAT 甲级 1041. Be Unique (20) 【STL】
题目链接 https://www.patest.cn/contests/pat-a-practise/1041 思路 可以用 map 标记 每个数字的出现次数 然后最后再 遍历一遍 找到那个 第一个 ...
- iOS 9 Safari广告拦截插件
相对于谷歌对广告拦截的禁止,苹果与之态度截然相反,继Mac版Safari加入广告拦截工具之后,即将到来的iOS9对Safari也引入了内容拦截插件-Content Blocker,并且开发者可以使用最 ...
- csrf防范笔记
1.验证Http的refer字段 http有一个refer字段,用以记录该http请求的来源地址 好处: 简单便捷,后台开发人员只需要设置一个拦截器 缺点: Referer 的值是由浏览器提供的,虽然 ...
- BZOJ 3990 [SDOI2015]排序
题解: 首先很容易看出各个操作是互不影响的,即对于一个合法的操作序列,我们可以任意交换两个操作的位置而不影响合法性. 因此我们可以忽略操作先后的影响,只考虑这个操作是否会出现在操作序列中. 如果用2n ...
- Git 使用初步
官网:https://git-scm.com/ 官方文档:https://git-scm.com/doc 比较简略的资料(对基本概念没有解释很清楚):http://wenku.baidu.com/li ...
- RQNOJ 202 奥运火炬登珠峰:01背包
题目链接:https://www.rqnoj.cn/problem/202 题意: 登珠峰需要携带a(L)O2和t(L)N2. 有n个气缸可供选择.其中第i个气缸能装下a[i](L)O2和t[i](L ...
- ThinkPHP基础(1)
多层MVC模式 M:Model 数据模型层,负责数据操作 V:View 视图层,负责显示视图 C:Controller 控制器,实现业务逻辑 控制器访问及路由解析 通过url地址get参数找到指定的控 ...
- CentOS7 网络管理工具nmcli
今天帮别人调试虚拟机的网络问题(CentOS 7系统),习惯性直接改/etc/sysconfig/network-scripts/ifcfg-xxx配置文件,但是不知道为什么重启network后静态i ...
- 如何将 Python 程序打包成 .exe 文件?
有不少订阅本公众号的朋友都不是玩 Python,甚至都不是计算机相关专业的,当我给他们一个 Python 程序时,他们是完全不知道该怎么运行的. 于是我想是不是可以将我的程序打包成可执行文件,直接运行 ...