抓住状态转移方程即可 : 从子序列 i j 中取最大 = i + 从子序列i+1,j中取最大 或 j + 从子序列i,j-1中取最大
#include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxn = 100+10; int S[maxn], A[maxn], d[maxn][maxn], vis[maxn][maxn], n; int dp(int i, int j){ if (vis[i][j]) return d[i][j]; vis[i][j] = 1; if (i == j) { d[i][j] = A[i]; return d[i][j]; } d[i][j] = max(A[i] + S[i+1,j] - dp(i + 1,j), A[j] + S[i,j - 1] - dp(i,j - 1)); return d[i][j]; } int main(){ while (scanf("%d", &n) && n){ S[0] = 0; for (int i = 1; i <= n;i++) { scanf("%d", &A[i]); S[i] = S[i - 1] + A[i]; } memset(vis, 0, sizeof(vis)); printf("%d\n", 2*dp(1, n)-S[n]); } return 0; }
时间: 2024-10-16 05:37:48