[Ybtoj high-efficiency advanced 1.3] C. Maximum mean [two points]

Insert picture description here
Insert picture description here

analysis

After
subtracting the average value of the two-point column , judge whether it is non-negative. If the
two-point value is subtracted, just add a small decimal every time.
Then update the max prefix and the new s[il] to take min each time

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

double jq=1e-5;
int n,c,d;
double l,r,a[100001],s[100001];

int pd(double x)
{
    
    
	double mi=1e10,ans=-1e10;
	memset(s,0,sizeof(s));
	for(int i=1;i<=n;i++)
	{
    
    
		s[i]=s[i-1]+a[i]-x;
	}
	for(int i=c;i<=n;i++)
	{
    
    
		mi=min(mi,s[i-c]);
		ans=max(ans,s[i]-mi);
	}
	return ans>=0;
}

int main()
{
    
    
	cin>>n>>c;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a[i];
	}
	l=-1e6;r=1e6;
	while(l+jq<r)
	{
    
    
		double mid=(l+r)/2;
		if(pd(mid)) l=mid;
		else r=mid;
	}
	d=r*1000;
	cout<<d;
	return 0;
} 

Guess you like

Origin blog.csdn.net/dglyr/article/details/112387233