A Game(区间DP)
Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.
Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy.
Input
The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)
The second line contains N integers A1, A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000)
Output
Output the maximum score Little Ho can get.
Sample Input
4
-1 0 100 2
Sample Output
99 官方题解 http://hihocoder.com/discuss/question/4984
/*
问题 两人轮流从一个数组的头或者尾选择一个数累加,并且将其删除,问先手累加的数最大是能多少
抽象问题 由于每次只能从数组首/尾拿走一个数,所以小Hi和小Ho在任何时候面对的残局都只可能是一段连续的子数组A[i..j]。
变量 i j 最多能获得的得分,其中i,j表示区间端点下标
定义状态 我们不妨用f[i][j]表示当面对A[i..j],先手最多能获得的得分。
并且如果我们能计算出所有f[i][j]的值,那么显然f[1][n]就是最终答案。
状态转移
i=j时,f[i][j]=A[i];
i<j时,此时先手面临A[i...j],若取A[i],则后手面临A[i+1...j],最多能得f[i+1][j]分,那么先手当前得分为
区间和减去后手最多得分(其实此时的后手也是先手,因为都符合每次都作出最优选择的条件)
即sum(A[i...j]) - f[i+1][j];
同理,若先手取A[j],则为sum(A[i...j]) - f[i][j-1]; 接下来就是写代码了(废话),我想说的是,状态方程虽然列出来了,怎么用也是需要思考的
我的做法是推理,要想计算f[i][j]需要先计算f[i+1][j]和f[i][j-1]
容易看出,i需要递减循环,j需要递增循环
故得出代码如下
*/
#include<stdio.h>
int max(int x, int y){
return x > y ? x : y;
}
int min(int x, int y){
return x > y ? y : x;
}
int n,A[],sum[],f[][];//大数组开成全局
int main()
{
int i,j;
while(scanf("%d",&n) != EOF)
{
sum[]=;
for(i=;i<=n;i++){
scanf("%d",&A[i]);
sum[i] = sum[i-] + A[i];
}
for(i=n;i>=;i--){
for(j=;j<=n;j++){
if(i == j)
f[i][j]=A[i];
else if(i < j)
f[i][j]=sum[j] - sum[i-] - min(f[i+][j],f[i][j-]);
//f[i][j]=max(sum[j]-sum[i-1] - f[i+1][j],sum[j]-sum[i-1] - f[i][j-1]);
//i > j无需处理
}
}
printf("%d\n",f[][n]);
}
return ;
}
A Game(区间DP)的更多相关文章
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
- 2016 年沈阳网络赛---QSC and Master(区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...
随机推荐
- AngularJS 表单数据验证及错误信息提示
一.表单验证基本原理 表单验证包括两个主题: 定义验证规则,验证数据有效性. 显示验证结果,把验证结果以友好的方式显示给用户. H5内置一些验证功能,并会显示内置的错误提示信息,先要禁用它,在< ...
- 关于国密算法 SM1,SM2,SM3,SM4 的笔记
国密即国家密码局认定的国产密码算法.主要有SM1,SM2,SM3,SM4.密钥长度和分组长度均为128位. SM1 为对称加密.其加密强度与AES相当.该算法不公开,调用该算法时,需要通过加密芯片的接 ...
- 2.Handler处理器 和 自定义Opener
Handler处理器 和 自定义Opener opener是 urllib2.OpenerDirector 的实例,我们之前一直都在使用的urlopen,它是一个特殊的opener(也就是模块帮我们构 ...
- hdu 1.2.6
勾股定理... #include<cstdio> #include<algorithm> using namespace std; int main() { //freopen ...
- Regular Expression
It's a very elegant summary of regular expression from The AWK Programming Language. 1. The regular ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 通过修改EIP寄存器实现远程注入
功能:通过修改EIP寄存器实现32位程序的DLL注入(如果是64位,记得自己对应修改汇编代码部分) 原理: 挂起目标进程,停止目标进程EIP的变换,在目标进程开启空间,然后把相关的指令机器码和数据拷贝 ...
- win7 docker 挂载共享目录
在 win7 下用 docker 不像 win10 那样方便,安装包都不一样. 在 win7 下共享一个目录的方法如下: 1. 先设置 win7 到 VirtualBox 中 docker 用的那个虚 ...
- 01-Python的基础知识3
- 数字 - 数字常量: - 整型: - 概念: - 指代平常数学上的整数常量.Python中整型指代int类型. - 基本运算: - 可以执行平常的+,-,*,/ ,%以及其他操作 假设a=15,b ...
- python互斥锁
互斥锁 进程之间数据隔离, 但是多个进程可以共享同一块数据,比如共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,而共享带来的是竞争,竞争带来的结果就是错乱,如下 from mu ...