具体看论文 浅谈数形结合思想在信息学竞赛中的应用
杭电现在交不了了。一直TLE,不过应该没错吧。poj的过了
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linxer, "/STACK:102400000,102400000")
#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 100010
#define M 200020
int getint(){
char ch=' ';
while(ch<'0'||ch>'9')ch=getchar();
int x=0;
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return x;
}
int a[N], sum[N], x[N], y[N], q[N];
LL cross(int ax, int ay, int bx, int by){
return 1LL * ax * by - 1LL * ay * bx;
}
bool check1(int i, int j, int k){
return (sum[k] - sum[j]) * (k - i) >= (sum[k] - sum[i]) * (k - j);
}
bool check2(int i, int j, int k){
return (sum[i] - sum[j]) * (j - k) <= (sum[j] - sum[k]) * (i - j);
}
int main(){
int n, k;
while(scanf("%d%d", &n, &k) != EOF){
for(int i = 1; i <= n; ++i)
a[i] = getint(), sum[i] = sum[i-1] + a[i];
int head = 0, tail = 0;
q[tail++] = 0;
double ans = 0;
for(int i = k; i <= n; ++i){
while(head + 1 < tail){
if(check1(q[head], q[head + 1], i)) head++;
else break;
}
ans = max(ans, 1.0 * (sum[i] - sum[q[head]]) / (i - q[head]));
int j = i - k + 1;
while(head + 1 < tail){
if(check2(j, q[tail-1], q[tail-2])) tail--;
else break;
}
q[tail++] = j;
}
printf("%.2f\n", ans);
}
return 0;
}