其他分享
首页 > 其他分享> > POJ Best Cow Fences

POJ Best Cow Fences

作者:互联网

POJ Best Cow Fences

题目:

题解:

#include <iostream>
#include <cstdio>
#include <cmath>
#define N 100005
using namespace std;

int n, L;
double l = 1e9, r = -1e9, ans;
double a[N], b[N], sum[N];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

bool check(double val)
{
    for(int i = 1; i <= n; i++)
        b[i] = a[i] - val,
        sum[i] = sum[i - 1] + b[i];
    double minn = 1e9, ans = -1e9;
    for(int i = L; i <= n; i++)
    {
        minn = min(minn, sum[i - L]);
        ans = max(ans, sum[i] - minn);  
    }
    return ans >= -1e-6; //这题卡精度
}

int main()
{
    freopen("P2018.in", "r", stdin);
    freopen("P2018.out", "w", stdout);
    
    cin >> n >> L;
    for(int i = 1; i <= n; i++)
    {
        a[i] = read();
        l = min(l, a[i]);
        r = max(r, a[i]);
    }
    for(int i = 1; i <= 60; i++)
    {
        double mid = (l + r) / 2.0;
        if(check(mid)) l = mid, ans = mid;
        else r = mid;
    }
    cout << (int)(ans * 1000.0);
    return 0;
}

标签:二分,Cow,int,平均数,sum,POJ,ans,include,Fences
来源: https://www.cnblogs.com/BigYellowDog/p/11269081.html