分蛋糕 2

分蛋糕 2

题解

很明显的一道区间dp,不知道笔者考场上会打错。我们定义dp_{l,r}为[l,r]区间还未被吃的总贡献值,再依次更新不同长度的区间即可。

dp方程式:

该IOI吃:dp_{l-1,r}=max(dp_{l,r},dp_{l-1,r}) \left ( a_{l}> a_{r} \right )

                dp_{l,r+1}=max(dp_{l,r},dp_{l,r+1}) \left ( a_{l}< a_{r} \right )

该JOI吃:dp_{l-1,r}=max(dp_{l,r}+a_{l},dp_{l-1,r}) 

                dp_{l,r+1}=max(dp_{l,r}+a_{r},dp_{l,r+1})

源码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
LL n,a[2005],ans;
LL dp[2005][2005];
#define gc() getchar()
template<typename _T>
inline void read(_T &x)
{
    _T f=1;x=0;char s=gc();
    while(s>'9'||s<'0'){if(s=='-')f=-1;s=gc();}
    while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=gc();}
    x*=f;
}
int main()
{
	read(n);
	for(int i=0;i<n;i++)
		read(a[i]);
	for(int i=n;i>=0;i--)
		for(int j=0;j<n;j++)
		{
			int l=j,r=(j-i+n+1)%n;
			if(i==0)
			{
				ans=max(dp[l][r],ans);
				continue;
			}
			if(n-i&1)
			{
				if(a[l]>a[r]) dp[(l-1+n)%n][r]=max(dp[l][r],dp[(l-1+n)%n][r]);
				else dp[l][(r+1)%n]=max(dp[l][r],dp[l][(r+1)%n]);
			}
			else
			{
				dp[(l-1+n)%n][r]=max(dp[l][r]+a[l],dp[(l-1+n)%n][r]);
				dp[l][(r+1)%n]=max(dp[l][r]+a[r],dp[l][(r+1)%n]);
			}
		}
	printf("%lld",ans);
    return 0;
}

谢谢!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值