A Game

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

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

輸入

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

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

輸出

Output the maximum score Little Ho can get.內存

樣例輸入
4
-1 0 100 2
樣例輸出
99
分析:枚舉區間長度和起點,動態規劃
代碼:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+10;
const int dis[][2]={0,1,-1,0,0,-1,1,0};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,dp[maxn][maxn],a[maxn],sum[maxn];
int main()
{
    int i,j,k,t;
    scanf("%d",&n);
    rep(i,1,n)scanf("%d",&a[i]),dp[i][1]=a[i],sum[i]=sum[i-1]+a[i];
    rep(i,2,n)
    {
        for(j=1;j+i-1<=n;j++)
        {
            dp[j][i]=max(sum[j+i-1]-sum[j-1]-dp[j][i-1],sum[j+i-1]-sum[j-1]-dp[j+1][i-1]);
        }
    }
    printf("%d\n",dp[1][n]);
    //system("pause");
    return 0;
}
相關文章
相關標籤/搜索