标签:应该 class 题解 思路 com def int math code
P2858 [USACO06FEB]Treats for the Cows G/S
\(dp\)。
\(dp[i][j]\) 表示现在已经取了 \(i\) 个并且有 \(j\) 个是从左边取得的最优解。
状态转移方程:
\(f[i][j] = max(f[i - 1][j] + a[n - i + j + 1] * i, f[i - 1][j - 1] + a[j] * i)\)
这应该不算是区间 \(dp\) 吧。。。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#define MAXN 2001
int n, ans, a[MAXN], f[MAXN][MAXN];
int max(int a, int b) { return a > b ? a : b; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
f[1][1] = a[1], f[1][0] = a[n];
for (int i = 2; i <= n; ++i) {
for (int j = 0; j <= i; ++j) {
if (j == 0) f[i][j] = f[i - 1][j] + a[n - i + 1] * i;
else f[i][j] = max(f[i - 1][j] + a[n - i + j + 1] * i, f[i - 1][j - 1] + a[j] * i);
}
}
for (int i = 0; i <= n; ++i) {
ans = max(ans, f[n][i]);
}
printf("%d\n", ans);
return 0;
}
【题解】P2858 [USACO06FEB]Treats for the Cows G/S
标签:应该 class 题解 思路 com def int math code
原文地址:https://www.cnblogs.com/poi-bolg-poi/p/13288606.html