poj2018(前缀和求最大连续子段+二分答案)

时间:2021-05-05 14:42:10
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const double nil=1e-5;
const int maxn=100000+10;
double a[maxn],b[maxn],sum[maxn];

int main(){
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    int n,L;
    scanf("%d%d",&n,&L);
    for (int i=1;i<=n;i++) scanf("%lf",&a[i]);
    double l=-1e6,r=1e6;
    while(r-l>nil){
        double mid=(l+r)/2;
        for (int i=1;i<=n;i++) b[i]=a[i]-mid;
        sum[1]=b[1];
        for (int i=2;i<=n;i++){
            sum[i]=sum[i-1]+b[i];
        }
        double ans=-1e10;
        double minn=0;
        ans=sum[L];
        for (int i=L+1;i<=n;i++){
            minn=min(minn,sum[i-L]);
            ans=max(ans,sum[i]-minn);
        }
        if(ans>=0) l=mid;
        else r=mid;
    }
    printf("%d\n",int(r*1000));
return 0;
}