Luogu P3594 [POI2015] WIL-Wilcze doły solution

Topic link

I’ve heard it before, but now I’ve all forgotten QwQ, hereby write a solution

First of all, in order to select the interval as long as possible, we have to change as many numbers as possible in the interval to 0 00 , and meet the elimination of the number and as large as possible.

We consider using dual pointers to maintain the interval [l, r] [l, r][l,r ] , and use a monotonic queue to maintain the length of the interval asddThe interval of d satisfies these intervals and is monotonically decreasing.
Then, ifsum (l, r) − sum (deleted interval) <p sum(l,r)-sum (deleted interval)<ps u m ( l ;r)S U m ( deleted to the region between )<p , then++r, otherwise++l

#include<cstdio>
#include<iostream>
#include<deque>
using namespace std;
const long long Maxn=2000000+10,inf=0x3f3f3f3f;
deque <long long> q;
long long n,m,k,ans;
long long a[Maxn],s[Maxn];
int main()
{
    
    
//	freopen("in.txt","r",stdin);
	scanf("%lld%lld%lld",&n,&k,&m);
	for(long long i=1;i<=n;++i)
	{
    
    
		scanf("%lld",a+i);
		s[i]=s[i-1]+a[i];
	}
	if(n<=m)
	{
    
    
		cout<<n<<endl;
		return 0;
	}
	long long l=1,r=min(n,m);
	q.push_back(1);
	while(l<=n && r<=n)
	{
    
    
		while(q.size() && q.front()<l)q.pop_front();
		long long i=q.front();
		if(s[r]-s[l-1]-(s[min(i+m-1,r)]-s[i-1])>k && r-l+1>m){
    
    ++l;continue;}
		ans=max(ans,r-l+1);
		++r;
		while(q.size() && s[q.back()+m-1]-s[q.back()-1]<=s[r]-s[r-m])
		q.pop_back();
		q.push_back(r-m+1);
	}
	printf("%lld\n",ans);
}

Guess you like

Origin blog.csdn.net/Brian_Pan_/article/details/107807513