Description
Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows <= 2000.
FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input.
Calculate the fence placement that maximizes the average, given the constraint.
Input
* Line 1: Two space-separated integers, N and F.
* Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.
Output
* Line 1: A single integer that is 1000 times the maximal average.Do not perform rounding, just print the integer that is 1000*ncows/nfields.
Sample Input
10 6 6 4 2 10 3 8 5 9 4 1
Sample Output
6500
Source
毒瘤题,用long double会比double慢一倍时间
枚举平均值,用原数列减去当前平均值,找到一个长度大于f的非负子段就可以了
#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll maxn = 1e5+100;
const ll mod = 1e9+7;
const ld pi = acos(-1.0);
const ll inf = 1e18;
const ld eps = 1e-5;
ll n,f;
ld arr[maxn],sum[maxn],sub[maxn];
ld l = inf,r = -inf;
/*bool check(ld x)
{
ld ans = -1e10,minval = 1e10;
for(ll i = 1; i <= n; i++)
{
sub[i] = arr[i]-x;
sum[i] = sum[i-1] + sub[i];
}
for(ll 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()
{
ios::sync_with_stdio(false);
//cin.tie(0),cout.tie(0);
//cout << eps << endl;
cin >> n >> f;
for(ll i = 1; i <= n; i++)
{
//scanf("%llf",&arr[i]);
cin >> arr[i];
//sum[i] = sum[i-1] + arr[i];
l = min(arr[i],l);
r = max(arr[i],r);
}
while(r-l > eps)
{
ld mid = (l+r)/2;
ld ans = -inf,minval = inf;
for(ll i = 1; i <= n; i++)
{
sub[i] = arr[i]-mid;
sum[i] = sum[i-1] + sub[i];
}
for(ll i = f; i <= n; i++) //一定大于f段
{
minval = min(minval,sum[i-f]);
ans = max(ans,sum[i]-minval);
}
if(ans >= 0)
l = mid;
else
r = mid;
}
cout << ll(r*1000) << endl;
return 0;
}