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

我们可以从最后往外推 当前状态就只由上一个状态决定

就是合唱队那道题改一下

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<stack>
 7 #include<algorithm>
 8 using namespace std;
 9 #define ll long long
10 #define rg register
11 const int N=2000+5,inf=0x3f3f3f3f,P=19650827;
12 int n,a[N],f[N][N];
13 template <class t>void rd(t &x)
14 {
15     x=0;int w=0;char ch=0;
16     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
17     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
18     x=w?-x:x;
19 }
20 
21 int main()
22 {
23     //freopen("in.txt","r",stdin);
24     //freopen("nocows.out","w",stdout);
25     rd(n);
26     for(rg int i=1;i<=n;++i) rd(a[i]),f[i][i]=a[i]*n;
27     for(rg int i=2;i<=n;++i)
28     {
29         for(rg int l=1;l<n&&l+i-1<=n;++l)
30         {
31             int r=l+i-1;
32             f[l][r]=max(f[l+1][r]+a[l]*(n-i+1),f[l][r-1]+a[r]*(n-i+1));
33         }
34     }
35     printf("%d",f[1][n]);
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/lxyyyy/p/10885990.html

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐