Best Cattle housing Acwing-102- (binary, real number)

link:

https://www.acwing.com/problem/content/104/

Meaning of the questions:

Farmer John's farm land by the N blocks, each block of the ground have a certain number of cattle, the number of no less than one nor more than 2000.

John fence would hope to use a portion surrounded successive fields, and such that the area enclosed bovine each comprise an average value of the maximum number.

At least comprising enclosing the block in the region F, where F will be given in the input.

The average number of maximum possible under the given conditions, calculating the area enclosed bovine each comprise a number is.

Ideas:

Half the maximum value, then the prefix and check again.

Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
double a[MAXN], b[MAXN], Sum[MAXN];
double eps = 1e-5;
int n, f;

bool Check(double mid)
{
    double minval = 1e10;
    double ans = -1e10;
    for (int i = f;i <= n;i++)
    {
        minval = min(minval, Sum[i-f]);
        ans = max(ans, Sum[i]-minval);
    }
    if (ans > 0)
        return true;
    return false;
}

int main()
{
    scanf("%d %d", &n, &f);
    for (int i = 1;i <= n;i++)
        scanf("%lf", &a[i]);
    double l = -1e6, r = 1e6;
    double ans = 0;
    while (r-l > eps)
    {
        double mid = (l+r)/2;
        for (int i = 1;i <= n;i++)
            b[i] = a[i]-mid;
        for (int i = 1;i <= n;i++)
            Sum[i] = Sum[i-1]+b[i];
        if (Check(mid))
        {
            l = mid;
            ans = max(ans, mid);
        }
        else
            r = mid;
    }
    printf("%d\n", int(r*1000));

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11469150.html