标签:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; const int INF = 1e9+7; const int MAXN = 255; int a[MAXN], dp[2][MAXN][MAXN]; int sum[MAXN]; int DFS(int cur,int L,int R) { if( dp[cur][L][R] != -INF ) return dp[cur][L][R]; if(L > R) return 0; if(L == R) return dp[cur][L][R] = a[L]; for(int i=L; i<=R; i++) { int A = sum[i]-sum[L-1];///所选取的区间 int B = sum[R]-sum[i];///丢弃的区间总和 dp[cur][L][R] = max(dp[cur][L][R],A + B - DFS(cur^1,i+1,R) ); } for(int i=R; i>=L; i--) { int A = sum[R] - sum[i-1]; int B = sum[i-1] - sum[L-1]; dp[cur][L][R] = max(dp[cur][L][R],A + B - DFS(cur^1,L,i-1)); } return dp[cur][L][R]; } int main() { int T, cas = 1, n; scanf("%d", &T); while(T --) { scanf("%d", &n); memset(sum, 0, sizeof(sum)); for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) dp[0][i][j] = dp[1][i][j] = -INF; for(int i=1; i<=n; i++) { scanf("%d", &a[i]); sum[i] = sum[i-1] + a[i]; } int ans = DFS(0, 1, n); printf("Case %d: %d\n", cas++, 2*ans - sum[n]); } return 0; } /* 3 3 1 2 3 */
Light OJ 1031 - Easy Game(区间DP)
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4911616.html