其他分享
首页 > 其他分享> > POJ - 2018 Best Cow Fences(二分)

POJ - 2018 Best Cow Fences(二分)

作者:互联网

POJ - 2018 Best Cow Fences

在这里插入图片描述

#include<iostream>
#include<cstdio>
using namespace std;

const int N = 100010;
const double eps = 1e-5;

int n,m;
int w[N];
double sum[N];

bool check(double avg)
{
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+w[i]-avg;

    double mins=0;
    for(int i=m,j=0;i<=n;i++,j++)
    {
        mins=min(mins,sum[j]);
        if(sum[i]>=mins) return true;
    }
    return false;
}

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++) scanf("%d",&w[i]);

    double l=0,r=2010;
    while(r-l>1e-5)
    {
        double mid=(l+r)/2;
        if(check(mid)) l=mid;
        else r=mid;
    }

    printf("%d\n",(int)(r*1000));
    return 0;
}

标签:return,Cow,int,double,sum,mid,POJ,2018,mins
来源: https://blog.csdn.net/qq_52792570/article/details/120910207