分蛋糕 2
题解
很明显的一道区间dp,不知道笔者考场上会打错。我们定义为[l,r]区间还未被吃的总贡献值,再依次更新不同长度的区间即可。
dp方程式:
该IOI吃:
该JOI吃:
源码
#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;
}