一道区间DP的水题 -- luogu P2858 [USACO06FEB]奶牛零食Treats for the Cows

https://www.luogu.org/problemnew/show/P2858

方程很好想,关键我多枚举了一次(不过也没多大关系)

#include <bits/stdc++.h>
#define read read()
#define up(i,l,r) for(register int i = (l);i <= (r); i++)
using namespace std;
const int N = 2005;
int n,a[N],f[N][N];
int read 
{
    int x = 0;char ch = getchar();
    while(ch < 48 || ch > 57) ch = getchar();
    while(ch >= 48 && ch <= 57) {x = 10 * x + ch - 48; ch = getchar();}
    return x;
}
//int mymax(int a,int b,int c){int x = max(a,b);int y = max(b,c); return max(x,y);}
int main()
{
    n = read; up(i,1,n) a[i] = read;
    up(i,1,n) f[i][i] = a[i]; 
    up(L,1,n)//枚举区间长度; 
        up(i,1, (n-L+1) ) //枚举左端点; 
        {
            int j = i + L - 1; int T = n + 1 - L;
            f[i][j] = max(f[i + 1][j] + a[i] * T,f[i][j - 1] + a[j] * T);//mymax(f[i][j],f[i + 1][j] + a[i] * T,f[i][j - 1] + a[j] * T);
        }
    printf("%d",f[1][n]);
    return 0;
}

 

posted @ 2019-01-25 08:47  陈星卿  阅读(112)  评论(0编辑  收藏  举报