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)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. 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 ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

  10. 2016 年沈阳网络赛---QSC and Master(区间DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...

随机推荐

  1. java基础知识-新手上路注意事项一

    1.文件名,类名,字节码文件名三者的关系 源文件名:Demo.java 类名:Demo 字节码文件名:Demo.class 2.如果class定义前,加上public修饰,说明这是一个公共类,此时类名 ...

  2. flume遇到的问题

    Caused by: java.lang.IllegalStateException: Unable to add FlumeEventPointer [fileID=, offset=]. Queu ...

  3. day01_雷神_Python入门

    day01 1.编程语言 主流的像C.java.python.php.C#.等,可以从不同维度分类如下: 机器码和字节码 机器码: C 字节码: 其他 note: 机器码是电脑的CPU可直接解读的数据 ...

  4. HRBUST 1161 树状数组区间更新求和

    Leyni Time Limit: 3000 MS Memory Limit: 65536 K Total Submit: 267(64 users) Total Accepted: 82(57 us ...

  5. LinuxC下获取UDP包中的路由目的IP地址和头标识目的地址

    在接受到UDP包后,有时候我们需要根据所接收到得UDP包,获取它的路由目的IP地址和头标识目的地址. (一)主要的步骤: 在setsockopt中设置IP_PKTINFO,然后通过recvmsg来获取 ...

  6. spark-mllib 密集向量和稀疏向量

    spark-mllib 密集向量和稀疏向量 MLlib支持局部向量和矩阵存储在单台服务器,也支持存储于一个或者多个rdd的分布式矩阵 . 局部向量和局部矩阵是用作公共接口的最简单的数据模型. 基本的线 ...

  7. c#中的gcAllowVeryLargeObjects和OutOfMemoryException

    什么是gcAllowVeryLargeObjects 在.net4.5中新增一个配置项 “gcAllowVeryLargeObjects” ,msdn解释如下: 在64位平台上,可以允许总共大于2千兆 ...

  8. PowerDesigner的Name和Code不同步设置

    1.“Tools”->"GeneralOptions"(最下方) 2.“Dialog”(左侧列表选第2个) 3.“Name to Code mirroring”的勾去掉

  9. Flask系列07--Flask中的CBV, 蓝图的CBV

    一.CBV使用 class base view 和django中类似 class Login(views.MethodView): # methods=["POST"," ...

  10. day 74 vue 2 axios数据请求 以及组件的学习

    前情提要:   vue 学习二: 一: 通过axios实现数据请求 1:json数据语法 json数据对象类似于JavaScript中的对象,但是它的键对应的值里面是没有函数方法的,值可以是普通变量, ...