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;
}
上一篇:2020Ubuntu server1804最新安装后的配置


下一篇:LeetCode lc111.二叉树的最小深度