POJ 2018 二分
题意:
思路:
二分一个答案 让整个数组都减掉它 判判有没有相距>len的逆序对
//By SiriusRen
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,len;
double a[100500],sum[100500],l=0x3ffffff,r,Mid,t[100500],minn[100500];
bool check(){
for(int i=1;i<=n;i++)t[i]=a[i]-Mid,sum[i]=sum[i-1]+t[i];
for(int i=1;i<=n;i++)minn[i]=min(minn[i-1],sum[i]);
for(int i=len;i<=n;i++)if(sum[i]-minn[i-len]>=0)return 1;
return 0;
}
int main(){
scanf("%d%d",&n,&len);
for(int i=1;i<=n;i++)scanf("%lf",&a[i]),r=max(r,a[i]),l=min(l,a[i]);
while(r-l>1e-5){
Mid=(l+r)/2;
if(check())l=Mid;
else r=Mid;
}
printf("%lld\n",(long long)(r*1000));
}