bzoj2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
题目链接
bzoj2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
题解
dp
代码
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c >'9') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c- '0',c = getchar();
return x * f;
}
const int maxn = 5005;
int c[maxn],dp[2][maxn][2],n;
int main() {
n = read(); for(int i = 1;i <= n;++ i) c[i] = read();
int now = 0;
for(int i = n;i >= 1;i --) {
now ^= 1;
memset(dp[now],0,sizeof dp[now]) ;
for(int j = 1;j <= n;++ j) dp[now][j][0] = dp[now][j][1] = 0;
for(int j = i;j <= n;++ j) {
dp[now][j][0] = max(dp[now ^ 1][j][1] + c[i],dp[now][j - 1][1] + c[j]);
dp[now][j][1] = min(dp[now ^ 1][j][0],dp[now][j - 1][0]);
}
}
printf("%d\n",dp[now][n][0]);
return 0;
}