做题记录整理二分2 P1419 寻找段落(2022/9/15)
P1419 寻找段落
裸的二分,可以当成一个练手题
#include<bits/stdc++.h>
#define for1(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int a[5000005],n,s,t;
double sum[5000005];
int q[5000005];
bool check(double x)
{
int hd=2,tl=1;
for1(i,1,n)
sum[i] = sum[i - 1] + (double)a[i] - x;
for1(i,s,n)//枚举l
{
while(hd<=tl&&sum[q[tl]]>sum[i-s]) tl--;
q[++tl]=i-s;
while(hd<=tl&&q[hd]<i-t) hd++;
if(hd<=tl&&sum[i]-sum[q[hd]]>=0) return true;
}
return false;
}
int main()
{
cin>>n>>s>>t;
for1(i,1,n) cin>>a[i];
double l=-100000,r=100000,ans=0;
while(r-l>1e-5)
{
double mid=(l+r)/2;
if(check(mid)) ans=mid,l=mid;
else r=mid;
}
printf("%.3f",ans);
return 0;
}

浙公网安备 33010602011771号