传送门:Codeforces 1197D
Problem Description
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=∑ai−k⌈(r−l+1)/m⌉, where ⌈x⌉ is the least integer greater than or equal to x.
The cost of empty subarray is equal to zero.
For example, if m=3, k=10 and a=[2,−4,15,−3,4,8,3], then the cost of some subarrays are:
a3…a3:15−k⌈1/3⌉=15−10=5;
a3…a4:(15−3)−k⌈2/3⌉=12−10=2;
a3…a5:(15−3+4)−k⌈3/3⌉=16−10=6;
a3…a6:(15−3+4+8)−k⌈4/3⌉=24−20=4;
a3…a7:(15−3+4+8+3)−k⌈5/3⌉=27−20=7.
Your task is to find the maximum cost of some subarray (possibly empty) of array a.
Input
The first line contains three integers n, m, and k (1≤n≤3⋅105,1≤m≤10,1≤k≤109).
The second line contains n integers a1,a2,…,an (−109≤ai≤109).
Output
Print the maximum cost of some subarray of array a.
Sample Input
7 3 10
2 -4 15 -3 4 8 3
5 2 1000
-13 -4 -9 -20 -11
Sample Output
7
0
题意:
给出一个序列,和m,k,求∑ai−k⌈(r−l+1)/m⌉最小(可以选择空数组)
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=350000;
ll dp[N];
ll sum[N];
ll n,m,k;
int main()
{
ll a;
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a);
sum[i]=sum[i-1]+a;
}
ll ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m&&j<=i;j++)
{
dp[i]=max(dp[i],sum[i]-sum[i-j]);
}
dp[i]-=k;
dp[i]=max(dp[i],0LL);
if(i>m)
{
dp[i]=max(dp[i],dp[i-m]+sum[i]-sum[i-m]-k);
}
ans=max(dp[i],ans);
}
printf("%I64d\n",ans);
return 0;
}