题目1 : 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
The second line contains N integers A1, A2, ... AN, denoting the array. (-1000 ≤ Ai
输出
Output the maximum score Little Ho can get.
样例输入
4 -1 0 100 2
样例输出
99
题意:有一个数组,小hi 和小ho要从中选一些数(小ho先选),规定只能选一头或一尾,问小ho能选的数的最大和。
题解:枚举区间的长度和起点,选的起点很重要。 区间DP。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1100;
int n,m,dp[maxn][maxn],a[maxn],sum[maxn];
int main()
{
int i,j,k,t;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]),dp[i][1]=a[i],sum[i]=sum[i-1]+a[i];
for(i=2;i<=n;i++)
{
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]);
return 0;
}