20分求解
查看原帖
20分求解
847125
_xiao_zy_lin楼主2023/12/21 20:21
#include<bits/stdc++.h>
using namespace std;
const int N=5e3+15;
long long a[N],dp[N][N],q[N],f[N];
int main()
{
	int n,m,s;
	scanf("%d%d%d",&n,&m,&s);
	for(int i=1;i<=n;i++)
	scanf("%lld",&a[i]);
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=m;j++)
		dp[i][j]=-1e16;
	}
	dp[0][0]=0;
	for(int i=1;i<=n;i++)
	{
		int hh=1,tt=1;
		f[hh]=dp[i-1][m];
		q[hh]=m;
		for(int j=m;j>=0;j--)
		{
			if(hh<=tt&&q[hh]>min(m,j+s-1))
			++hh;
			while(hh<=tt&&f[tt]<dp[i-1][j-1])
			--tt;
			q[++tt]=j-1;
			f[tt]=dp[i-1][j-1];
			dp[i][j]=f[hh]+a[i]*j;
		}
	}
	long long ans=-1e16;
	for(int j=0;j<=m;j++)
	ans=max(ans,dp[n][j]);
	printf("%lld",ans);
}
2023/12/21 20:21
加载中...