poj2018 Best Cow Fences

二分答案+判定

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const double eps=1e-5,inf=999999999;
int n,ll;
double t[maxn],a[maxn],sum[maxn];
bool check(double k){
    for(int i=1;i<=n;++i){
        t[i]=a[i]-k;
        sum[i]=sum[i-1]+t[i];
    }
    double minx=0;
    for(int i=ll;i<=n;++i){
        if(sum[i]-minx+eps>=0)return 1;
        minx=min(minx,sum[i-ll+1]);
    }
    return 0;
}
int main(){
    double l=0,r=0,ans;
    scanf("%d%d",&n,&ll);
    for(int i=1;i<=n;++i){
        scanf("%lf",&a[i]);
        r=max(r,a[i]);
    }
    while(r-l>eps){
        double mid=(l+r)/2.0;
        if(check(mid)){
            l=mid;
            ans=mid;
        }
        else r=mid;
    }
    printf("%d",(int)((ans+eps)*1000));
    return 0;
}
View Code

 

posted @ 2018-12-17 10:30  lqsno1  阅读(47)  评论(0编辑  收藏  举报