30 August
DP 复习。
参考 redbag 博客 提供的题表。
P2858 [USACO06FEB] Treats for the Cows
区间 DP。
转换思路,题面从外往里递推,我们采用从里往外递推,权值逐级递减的反向实现方式。
选择区间左端点或右端点更新答案。
int n, a[2003], f[2003][2003];
int main() {
scanf("%d", &n);
for (int i=1; i<=n; ++i) scanf("%d", &a[i]), f[i][i]=a[i]*n;
for (int k=1; k<n; ++k) for (int i=1; i+k<=n; ++i)
f[i][i+k]=max(f[i+1][i+k] + a[i]*(n-k), f[i][i+k-1] + a[i+k]*(n-k));
printf("%d\n", f[1][n]);
return 0;
}
P2867 [USACO06NOV] Big Square
多重背包。
Post author 作者: Grey
Copyright Notice 版权说明: Except where otherwise noted, all content of this blog is licensed under a CC BY-NC-SA 4.0 International license. 除非另有说明,本博客上的所有文章均受 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 保护。