Description
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
* 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
Sample Input
10 6
6
4
2
10
3
8
5
9
4
1
Sample Output
6500
【题意】给出n个数,在这n个数里面找到一些连续的数,这些数的数量大于等于m,并且他们的平均值在这n个数里面最大
【思路】先把n个数的最大最小值确定,然后二分枚举平均值,对于每一个连续数,只要他们减去平均值大于0,就调制上限制,不然调整下限制,.......
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
double a[N],sum[N];
int n,m;
int check(double k)
{
double tmp=sum[m-]-(m-)*k;
for(int i=m;i<=n;i++)
{
tmp+=a[i]-k;
tmp=max(tmp,sum[i]-sum[i-m]-m*k);//新选m个数和前面的数比较,取最大值
if(tmp>-1e-)
return ;
}
return ;
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
sum[]=;
double maxn=,minx=inf;
for(int i=;i<=n;i++)
{
scanf("%lf",&a[i]);
if(a[i]>maxn)
maxn=a[i];
if(a[i]<minx)
minx=a[i];
sum[i]=sum[i-]+a[i];
}
while(maxn-minx>=1e-)
{
double mid=(maxn+minx)/;
if(check(mid))
minx=mid;
else maxn=mid;
}
int ans=*maxn;
printf("%d\n",ans);
}
return ;
}