直接看论文即可做
给个论文链接:点击打开链接里面的例二就是
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX 0x3f3f3f3f
double sum[110000], ans;
int pos[110000], s, e;
int N, K;
int getInt(){//没这个这题过不了
char ch = getchar();
while( ch < '0' || ch > '9' ) ch = getchar();
int num = 0;
while( ch >= '0' && ch <= '9' ){
num = num * 10 + ch - '0';
ch = getchar();
}
return num;
}
int main(){
while( scanf( "%d%d", &N, &K ) != EOF ){
sum[0] = 0;
for( int i = 1; i <= N; i++ ){
int temp = getInt();
sum[i] = temp + sum[i-1];
}
s = 0, e = -1;
ans = -MAX;
for( int i = 0; i + K <= N; i++ ){
while( s < e ){
double k1 = ( sum[i] - sum[pos[e]] ) / ( i - pos[e] );
double k2 = ( sum[pos[e]] - sum[pos[e-1]] ) / ( pos[e] - pos[e-1] );
if( k2 >= k1 ){
e--;
}else{
break;
}
}
pos[++e] = i;
while( s < e ){
double k1 = ( sum[i+K] - sum[pos[s]] ) / ( i + K - pos[s] );
double k2 = ( sum[i+K] - sum[pos[s+1]] ) / ( i + K - pos[s+1] );
if( k2 >= k1 ){
s++;
}else{
break;
}
}
double temp = ( sum[i+K] - sum[pos[s]] ) / ( i + K - pos[s] );
ans = max( ans, temp );
}
printf( "%.2lf\n", ans );
}
return 0;
}