CodeForces 1197 D Yet Another Subarray Problem

Face questions

    CF have to say this was very good at a first glance people scratching their heads and then think about the problem of finding a hhh SB theme (your own punctuation).

    Provided SUM [] array and the prefix, then the interval [l, r] value of sum [r] - sum [l-1] - rounding ([r- (l-1)] / m) on the k *.

    Or represents [l + 1, r] value of some more simple: sum [r] - sum [l] - rounding the k * ((rl) / m).

    Section indicates what is not important, we only care about the maximum value of the latter, when r is determined, the value is only rounding ((rl) / m) associated with the [l] + a sum k *.

    Our approach is similar to the scanning lines, each scan line is shifted one bit to the right (r -> r + 1), and see what happens:

        We found that all (l% m) == (r% m) corresponding to the sum of the rounding l [l] + k a * ((rl) / m) are large k, and also more than a value of r plus incoming, the other value corresponding to l not changed.

    So we directly open a minimum set of records about the congruence coefficient at a% m meaning to each equivalence class, maintenance is O (1), the query O (m).

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=300005;

ll a[N],mn[10],ans;
int n,m,k;

inline void solve(const int M){
	memset(mn,0x7f,sizeof(mn));
	mn[0]=k;
	
	for(int i=1,j=1;i<=n;j++,i++){
		ans=max(ans,a[i]-*min_element(mn,mn+M));
		if(j>=M) j-=M;
		mn[j]=min(mn[j],a[i])+k;
	}
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++) scanf("%lld",a+i),a[i]+=a[i-1];
	solve(m),printf("%lld\n",ans);
	return 0;
}

  

    

Guess you like

Origin www.cnblogs.com/JYYHH/p/11258436.html