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. bootstrap2.2登录验证

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. java 图片压缩 缩放

    废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的 /** * 图片缩放(未考虑多种图片格式和等比例缩放) * @param filePath 图片路径 * @param height 高度 ...

  3. 【Android开发那点破事】打开APP加载页面实现

    今天的破事呢就说说APP加载页面的实现.一般情况下,当APP打开的时候,我们需要做很多事情,比如检查网络连接啊,初始化一些配置啊等等.我们可以让这些事情在APP完全打开之前做完,然后呢在打开的过程中显 ...

  4. J - Oil Skimming 二分图的最大匹配

    Description Thanks to a certain "green" resources company, there is a new profitable indus ...

  5. CentOS 7.2通过yum安装MairaDB 10.1

    CentOS 7.2自带的yum源中mysql已经被替换成了mariadb,然而却是5.5版本,匹配mysql5.5,想要使用mysql 5.7的特性需要mariadb 10.0或10.1版本,10. ...

  6. SignalR 2 入门

    在本教程中使用的软件版本 Visual Studio 2015 .NET 4.5 SignalR 版本 2 概述 本教程介绍了通过演示如何生成简单的基于浏览器的聊天应用程序的 SignalR 开发. ...

  7. devexpress gridview 添加按钮

    #region 添加按钮事件 private RepositoryItemButtonEdit CreateRepositoryItemButtonEdit(Dictionary<object, ...

  8. WPF 打印界面(控件)到A4纸

    这次遇到一个需求,就是将整个界面打印在A4纸上. 需求清楚后,Bing一下关于打印,就找到一个类PrintDialog ,其中两个方法可能会用到: 特别是public void PrintVisual ...

  9. Download SQL Server Management Studio (SSMS)下载地址

    Download SQL Server Management Studio (SSMS)下载地址: https://msdn.microsoft.com/en-us/library/mt238290. ...

  10. C# Windows Service中执行死循环轮询

    用C#编写Windows Service时,执行轮询一般有两种方式,一种是用Timer,System.Timers或者是System.Thread下的,这种执行是按时间循环执行,缺点是也许上个执行还没 ...