Title: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461
This problem is a very good linear DP topic, this double head can be selected, can be used with the head, tail position information of the state
DP[I][J]: = The optimal solution of a sub-problem with the beginning of the first position and the end of the J-bit
DP[I][J] = max (sum (i, I+K1) - dp[i+k1+1][j], sum (i, J-K2) - dp[i][j-k2-1]) (When I <= J, note: I+k1 <= J && j-k2 >= i)
= 0 (When i > J)
#include <cstdio>#include<iostream>#include<cstring>using namespacestd;inta[ the];intdp[ the][ the];intvis[ the][ the];intFintLintr) {if(L > R)return 0; if(Vis[l][r])returnDp[l][r]; intRET =0, sum =0; for(intI=l; i<=r; i++) ret + =A[i]; for(intI=l; i<=r; i++) {sum+=A[i]; RET= Max (ret, sum-f (i+1, R)); } Sum=0; for(intk=0; i>=l; i--) {sum+=A[i]; RET= Max (ret, sum-f (L, I-1)); } Vis[l][r]=1; returnDP[L][R] =ret;}intMain () {intN; while(SCANF ("%d", &n)! = EOF &&N) { for(intI=0; i<n; i++) scanf ("%d", &A[i]); memset (Vis,0,sizeof(VIS)); printf ("%d\n", F (0, N-1)); } return 0;}
Uva-10891--game of Sum