【luogu2858】 [USACO06FEB]奶牛零食Treats for the Cows [动态规划 区间dp]

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值