The average number of hackers

Left the old long time, and finally went back to the room, a little excited, vowed to learn, I believe in the calm after nearly four months so I will not be like before the water, took an hour to write this question, but look, all right slowly to the solution of the problem do not worry.

Questions surface:

   Sequence of length n is given, find the length not less than k consecutive sequence, and this sequence is the maximum average value.

   The maximum average output (reserved six decimal)

answer:

  1) lower version (n, k <= 5000) to find a maximum value of about enumeration endpoint

  2) high version (n, k <= 1e5, a [i] <5000) bipartite  

    For each of the two check value for mid, more preferably if the solution exists, then:

            

    We then each a [i] by subtracting a mid, thus establishing a check aa [i] = a [i] -mid, b [i] = b [i-1] + aa [i],

    Into question: whether there is a requirement to meet the length of the section and not equal to zero.

    An array can be established to achieve f, f [i] indicates that the update f [i] and the ans sequence in a continuous process at the end of the maximum average value i, sweeping from left to right again, if ans is greater than 0, there is better solution.

    

#include <bits/stdc++.h>
using namespace std;
inline double read()
{
    char ch=getchar();
    int s=0,f=0;
    while(!(ch<='9'&&ch>='0')) {f|=ch=='-';ch=getchar();}
    while(ch<='9'&&ch>='0') {s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
    return f?-s:s;
}
int n, k;
double l=1e9, r=-1e9, ans, mid;
double f[10004],aa[10004],b[10004],a[10004];
bool check(double x)
{
    ans = -1e9; 
    for(int i=1;i<=n;i++)
        aa[i] = a[i]-x, b[i] = b[i-1]+aa[i];
    
    memset(f,-10,sizeof(F));
     for ( int i = K; i <= n-; i ++) // Enumeration right end points, f [i] denotes the end of a continuous subsequence of the maximum average i 
        f [i] = max (f [I- . 1 ] + AA [I], B [I] -b [IK]), ANS = max (ANS, F [I]); 
        
    IF (ANS> = 0 ) return  to true ;
     the else  return  to false ; 
    
} 
int main () 
{ 
    n- = Read (); K = Read ();
     for ( int I = . 1 ; I <= n-; I ++ ) 
        A [I] = Read (), L = min (L, A [I]) , R & lt = max (R & lt, a [I]); 
    // find the answer in the range of L, R & lt 
    the while(fabs(r-l)>1e-10)
    {
        mid = (r+l)/2;
        if(check(mid)) l = mid;
        else r = mid; 
    }
    printf("%.6lf\n",l);
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lyflalala/p/11323330.html