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<bits/stdc++.h>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值