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.
Entry
* 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.
Export
* 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.
Problem-solving ideas: Title mean length greater than or equal to find the maximum continuous average F I start from 1e6 ~ 1e6 half of each binary number has been subtracted from averaging the prefix and the length L where start determination look before subtracting the minimum value is not greater than 0 to iL
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define eps 1e-6 6 using namespace std; 7 const double INF=0x3f3f3f3f*1.0; 8 int n,m; 9 double arr[100005]; 10 double sum[100005]; 11 12 bool check(double num){ 13 double minn=INF; 14 for ( int I = . 1 ; I <= n-; I ++ ) { 15 SUM [I] = SUM [I- . 1 ] + ARR [I] - NUM; 16 } . 17 Double ANS; 18 is for ( int I = m; I <= n-; I ++) { /// See length no greater than equal to m are greater than or equal to 0 . 19 Minn = min (Minn, SUM [IM]); // minimum previous IM 20 is ANS = SUM [ I] -m; // 21 is IF (ANS> = 0 ) return to true ; // this is achievable average 22 } 23 return false; 24 } 25 26 void Calculation(){ 27 double left=1e-6; 28 double right=1e6; 29 while(left+eps<right){ 30 double mid=(left+right)/2; 31 if(check(mid)) left=mid; 32 else right=mid; 33 } 34 printf("%d\n",(int)(right*1000)); 35 } 36 37 38 int main(){ 39 scanf("%d%d",&n,&m); 40 for(int i=1;i<=n;i++) scanf("%lf",&arr[i]); 41 Calculation(); 42 return 0; 43 }