lightoj 1031 区间dp
题目链接: http://lightoj.com/volume_showproblem.php?problem=1031
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 105; int dp[maxn][maxn]; //dp[i][j] 表示先手从i到j比后手多的分差。 int sum[maxn],a[maxn]; int N; int main() { // freopen("E:\\acm\\input.txt","r",stdin); int T; cin>>T; for(int cas=1;cas<=T;cas++){ scanf("%d",&N); sum[0] = 0; memset(dp,-0x3f,sizeof(dp)); for(int i=1;i<=N;i++){ int a; scanf("%d",&a); sum[i] = sum[i-1] + a; dp[i][i] = a; } for(int i=1;i<=N+1;i++) dp[i][i-1] = 0; for(int i=N-1;i>=1;i--) for(int j=i+1;j<=N;j++){ for(int k=i;k<=j;k++){ if(k == j){ dp[i][j] = max(dp[i][j],sum[j]-sum[i-1]); //不能取零个,这是取全部的情况。 continue; } dp[i][j] = max(dp[i][j],max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k])); } } printf("Case %d: %d\n",cas,dp[1][N]); } }