P2858 [USACO06FEB]奶牛零食Treats for the Cows
区间dp,级像矩阵取数,
f[i][i+l]=max(f[i+1][i+l]+a[i]*(m-l),f[i][i+l-1]+a[i+l]*(m-l));
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<cstring> 8 #define inf 2147483647 9 #define For(i,a,b) for(register int i=a;i<=b;i++) 10 #define p(a) putchar(a) 11 #define g() getchar() 12 //by war 13 //2017.10.19 14 using namespace std; 15 int n,m; 16 int a[2010]; 17 int f[2010][2010]; 18 int Max; 19 int ans; 20 void in(int &x) 21 { 22 int y=1; 23 char c=g();x=0; 24 while(c<'0'||c>'9') 25 { 26 if(c=='-') 27 y=-1; 28 c=g(); 29 } 30 while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); 31 x*=y; 32 } 33 void o(int x) 34 { 35 if(x<0) 36 { 37 p('-'); 38 x=-x; 39 } 40 if(x>9)o(x/10); 41 p(x%10+'0'); 42 } 43 int main() 44 { 45 in(m); 46 For(jj,1,m) 47 in(a[jj]); 48 For(i,1,m) 49 f[i][i]=m*a[i]; 50 For(l,1,m-1) 51 For(i,1,m-l) 52 f[i][i+l]=max(f[i+1][i+l]+a[i]*(m-l),f[i][i+l-1]+a[i+l]*(m-l)); 53 o(f[1][m]); 54 return 0; 55 }