洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows
题目传送门
解题思路:
f[i][j]表示区间i~j可获得的最大利润,则每个区间可能从两个状态转移过来,即我上一次卖了第i-1个或第j+1个.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int n,v[2001],f[2001][2001],ans; 7 8 int main() { 9 scanf("%d",&n); 10 for(int i = 1;i <= n; i++) 11 scanf("%d",&v[i]); 12 for(int i = 1;i <= n; i++) 13 for(int j = n;j >= i; j--) { 14 f[i][j] = max(f[i][j],f[i-1][j] + v[i-1] * (n - j + i - 1)); 15 f[i][j] = max(f[i][j],f[i][j+1] + v[j+1] * (n - j + i - 1)); 16 } 17 for(int i = 1;i <= n; i++) 18 ans = max(ans,f[i][i] + v[i] * n); 19 printf("%d",ans); 20 return 0; 21 }