[Ybtoj high-efficiency advanced 1.3] [two points] maximum mean

[Ybtoj high-efficiency advanced 1.3] [two points] maximum mean

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

Bipartite enumeration average
calculated after subtracting the average number of all prefixes and
if the period of non-negative segment and
description of this interval evaluation value larger than the current mid
double split cycle direct enumeration range interval
conceivable not long The interval
is smaller than L, so if a smaller value is subtracted, the sum of the intervals will be better. You
only need to know the smallest value from 1 to iL. You
only need the end of the enumeration period.


Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-5;
int n,c,da;
double l,r,a[100010],sum[100010];
bool pd(double x)
{
    
    
	 double mi=1e10,ans=-1e10;
	 memset(sum,0,sizeof(sum));
	 for (int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]-x;  //求前缀和
	 for (int i=c;i<=n;i++)
	 {
    
    
	 	 mi=min(mi,sum[i-c]);  //找最小的值
	 	 ans=max(ans,sum[i]-mi);  //更新答案
	 } 
	 return ans>=0;  
}
int main()
{
    
    
	scanf("%d%d",&n,&c);
	for (int i=1;i<=n;i++)
	    cin>>a[i];
	l=-1e6,r=1e6;
	while (l+eps<r)
    {
    
     
          double mid=(l+r)/2; 
		  if (pd(mid))
		     l=mid;
			 else r=mid;
	} 
	da=r*1000;
	printf("%d\n",da);
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/112112419