【hihocoder 1388】【区间DP】A Game【给出一个数列,两人每次从数列的头尾取数,小ho 先取,小hi每次都采取最优策略,问小ho最终能取得的数的总和最大是多少】

来源:互联网 发布:apache mpm 配置 编辑:程序博客网 时间:2024/05/19 16:48

传送门:http://hihocoder.com/problemset/problem/1338

题意:小hi和小ho玩游戏,给出一个数列,两人每次从数列的头尾取数,小ho 先取,小hi每次都采取最优策略,问小ho最终能取得的数的总和最大是多少。

思路:

区间动规

dp[i][l]表示当前某个人以最优策略在i为开头,l为长度的区间上执先手选数的最大答案,

转移方程为dp[i][l] = max(sum[i + l - 1] - sum[i - 1] - dp[i][l - 1], sum[i + l - 1] - sum[i - 1] - dp[i + 1][l - 1])(小ho取数组尾的数,小ho取数组头的数)

然后用前缀和优化一下复杂度为O(n^2)


代码:

#include <iostream>#include <stdio.h>using  namespace  std;#define rep(i,k,n) for(int i=k;i<=n;i++)template<class T> void read(T&num) {    char CH; bool F=false;    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());    F && (num=-num);}const int N=1e3+10;int n, a[N], sum[N], dp[N][N];int  main(){  read(n);  rep(i, 1, n)    read(a[i]), sum[i] = sum[i - 1] + a[i], dp[i][1] = a[i];  for(int l = 2; l <= n; l++){    for(int i = 1; i + l - 1 <= n; i++){      dp[i][l] = max(sum[i + l - 1] - sum[i - 1] - dp[i][l - 1], sum[i + l - 1] - sum[i - 1] - dp[i + 1][l - 1]);    }  }  printf("%d\n", dp[1][n]);  return 0;}  

描述:

#1338 : A Game

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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. 

输入

The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)

The second line contains N integers A1A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000)

输出

Output the maximum score Little Ho can get.

样例输入
4-1 0 100 2
样例输出
99

0 0
原创粉丝点击