Title Link: http://www.lightoj.com/volume_showproblem.php?problem=1031
Title: Two players, alternately, can be taken from either end of the array, each time can go to any but only one end, their score is to get all the values and. Now ask for the maximum value of the two players ' score difference.
Problem-solving ideas: Set DP[I][J] for the interval from I to J, the maximum difference that can be obtained, only need to enumerate one of the number C between I and J, as a power outage, then the maximum value should be Max (Sum[c]-sum[i-1]-dp[c+1][j], sum[j]-sum[ C-1]-cou (i, c-1)). Then update this value on the line.
The code is as follows:
#include <bits/stdc++.h>
using namespace std;
const int N = 107;
int sum[N], dp[N][N];
int cou(int i, int j)
{
if(i > j)
return 0;
if(dp[i][j] != INT_MAX)
return dp[i][j];
dp[i][j] = sum[j] - sum[i-1];
for(int c=i; c<=j; ++ c)
{
dp[i][j] = max(dp[i][j], sum[c] - sum[i-1] - cou(c+1, j));
dp[i][j] = max(dp[i][j], sum[j] - sum[c-1] - cou(i, c-1));
}
return dp[i][j];
}
void solve(int cases)
{
int n;
scanf("%d", &n);
for(int i=1; i<=n; ++ i)
for(int j=1; j<=n; ++ j)
dp[i][j] = INT_MAX;
sum[0] = 0;
for(int i=1; i<=n; ++ i)
{
scanf("%d", &sum[i]);
dp[i][i] = sum[i];
sum[i] += sum[i-1];
}
printf("Case %d: %d\n", cases, cou(1, n));
}
int main()
{
int t;
scanf("%d", &t);
for(int i=1; i<=t; ++ i)
solve(i);
return 0;
}
Light OJ 1031-easy Game (interval dp)