Educational Codeforces Round 69 D Yet Another Subarray Problem

D Yet Another Subarray Problem

Thinking

  • Meet this maximum demand period interval condition, a feature of this equation is, increasing the length of the interval m, minus only a k, m and title data range is small.
  • Enumeration can be considered a starting point, but then we enumerate the starting point, the back of the update do not know how quick update to solve.
  • Less than the number of enumerated m, 0 ~ m-1, can be seen as enumerated before m-1 position. After the starting point for each additional m number of positions, sum and you need to lose k. Minn maintain a minimum (the initial value is set to 0, because of not taking the maximum, it is 0). During subsequent enumeration, each time before the need to lose k, update the look Minn, taking the minimum value of the preceding paragraphs. Enumeration at each updating answer res = max (res, sum-minn), represents the current prefix and - the prefix in front of a certain minimum value and to update answers.
  • Why not update minn at each point of it? Because the operation is to appear every -k m, the same can not be repeatedly over a period of -k, if each point Minn update, then the same period interval, sum-minn eliminates the need to reduce the value of k. Sample example 2, -4, 15.

Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[300005];
int main()
{
    int n, m, k;
    ll res = 0;
    scanf("%d%d%d", &n,&m,&k);
    for(int i=0;i<n;i++)scanf("%lld",&a[i]);

    for(int i=0;i<m;i++){
        ll minn=0;//最大为0
        ll sum=0;
        for(int j=i;j<n;j++){
            if(j%m==i){
                minn=min(sum,minn);
                sum-=k;
            }
            sum+=a[j];
            res=max(res,sum-minn);
        }

    }
    printf("%lld\n", res);
}

Guess you like

Origin www.cnblogs.com/gzr2018/p/11235913.html