Yet Another Subarray Problem Codeforces Round 69 dp

链接: http://codeforces.com/contest/1197/problem/D
题面:
You are given an array a1,a2,…,an and two integers m and k.

You can choose some subarray al,al+1,…,ar−1,ar.

The cost of subarray al,al+1,…,ar−1,ar is equal to ∑ i = l r a i − k ⌈ ( r − l + 1 ) m ⌉ ∑_{i=l}^{r}a_{i}−k⌈\frac{(r−l+1)}m⌉ i=lraikm(rl+1), where ⌈x⌉ is the least integer greater than or equal to x.

The cost of empty subarray is equal to zero.
Your task is to find the maximum cost of some subarray (possibly empty) of array a.
题意: 找出一段区间使得 ∑ i = l r a i − k ⌈ ( r − l + 1 ) m ⌉ ∑_{i=l}^{r}a_{i}−k⌈\frac{(r−l+1)}m⌉ i=lraikm(rl+1)最大,区间长度可以为0,即不选
思路: dp[ i ]为前i个能得到的最大值,每m个减去k

#include
using namespace std;
const int N=3e5+5;
typedef long long ll;
ll a[N];
ll dp[N];
int main()
{
	int n,m,k;
	ll ans=0;
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		dp[i]=max(a[i]-k,dp[i]);
		a[i]+=a[i-1];
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(i-j>=0)
			{
				dp[i]=max(dp[i],dp[i-j]-k+a[i]-a[i-j]);
				ans=max(ans,dp[i]);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

你可能感兴趣的:(codeforces,DP)