bzoj1652 / P2858 [USACO06FEB]奶牛零食Treats for the Cows

P2858 [USACO06FEB]奶牛零食Treats for the Cows

区间dp

设$f[l][r]$为取区间$[l,r]$的最优解,蓝后倒着推

$f[l][r]=max(f[l+1][r]+a[l]*p,f[l][r-1]+a[r]*p)$

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int max(int a,int b){return a>b?a:b;}
 6 #define N 2010
 7 int a[N],f[N][N],n;
 8 int main(){
 9     //freopen("P2858.in","r",stdin);
10     scanf("%d",&n);
11     for(int i=1;i<=n;++i)
12         scanf("%d",&a[i]),f[i][i]=a[i]*n;
13     for(int i=2;i<=n;++i)
14         for(int l=1,r,p;l+i-1<=n;++l){
15             r=l+i-1; p=n-i+1;
16             f[l][r]=max(f[l+1][r]+a[l]*p,f[l][r-1]+a[r]*p);
17         }
18     printf("%d",f[1][n]);
19     return 0;
20 }
View Code

 

posted @ 2018-11-30 11:01  kafuuchino  阅读(150)  评论(0编辑  收藏  举报