MAX Average Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7639 Accepted Submission(s): 1667
Problem Description
Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s calculate max(ave(i,j)), 1<=i<=j-k+1<=n.
Input
There multiple test cases in the input, each test case contains two lines.
The first line has two integers, N and k (k<=N<=10^5).
The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].
The first line has two integers, N and k (k<=N<=10^5).
The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].
Output
For every test case, output one single line contains a real number, which is mentioned in the description, accurate to 0.01.
Sample Input
10 6 6 4 2 10 3 8 5 9 4 1
Sample Output
6.50
这题有个模型,挺经典的。
然而,不知为何,HDU上这道题已经不能AC了,原来的标程都会TLE!!!
所以我就把我的TLE的程序当标程挂在这了~~~
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 int sum[1000010],q[1000010]; 8 int n,L; 9 double mid; 10 double C(int i) 11 { 12 return sum[i]*1.0-mid*i; 13 } 14 int h,t; 15 bool IS_OK(double x) 16 { 17 h=1;t=0; 18 for(int i=L;i<=n;i++) 19 { 20 while(h<=t&&C(q[t])<=C(i))t--; 21 q[++t]=i; 22 } 23 for(int i=1;i<=n-L+1;i++) 24 { 25 if(q[h]-i+1<L)h++; 26 if(C(q[h])-C(i-1)>=0) 27 return true; 28 } 29 return false; 30 } 31 int main() 32 { 33 double lo,hi; 34 while(~scanf("%d%d",&n,&L)){ 35 lo=1.0,hi=2000.0; 36 for(int i=1;i<=n;i++){ 37 scanf("%d",&sum[i]); 38 lo=min(sum[i]*1.0,lo); 39 hi=max(sum[i]*1.0,hi); 40 } 41 42 for(int i=2;i<=n;i++) 43 sum[i]+=sum[i-1]; 44 45 46 while(hi-lo>=0.01) 47 { 48 mid=(lo+hi)/2.0; 49 if(IS_OK(mid))lo=mid; 50 else hi=mid; 51 } 52 printf("%.2f\n",hi); 53 } 54 return 0; 55 }