题解
- 先二分出来一个平均值,然后将序列里的所有数减去二分出来的数
- 然后求一遍前缀和直接判断就好了
代码
1 #include <cstdio>
2 #include <cstring>
3 #define N 300010
4 using namespace std;
5 int n,k;
6 double l,r,mid,a[N],b[N],eps=1e-5;
7 bool check(int x)
8 {
9 double p=0;
10 for (int i=k;i<=n;i++)
11 {
12 if (b[i]-p>=0) return 1;
13 if (b[i-k+1]<p) p=b[i-k+1];
14 }
15 return 0;
16 }
17 int main()
18 {
19 freopen("average.in","r",stdin),freopen("average.out","w",stdout),scanf("%d%d",&n,&k);
20 for (int i=1;i<=n;i++) scanf("%lf",&a[i]),r+=a[i];
21 while (r-l>eps)
22 {
23 double mid=(l+r)/2.0; b[0]=0;
24 for (int i=1;i<=n;i++) a[i]-=mid,b[i]=b[i-1]+a[i];
25 if (check(mid)) l=mid; else r=mid;
26 for (int i=1;i<=n;i++) a[i]+=mid;
27 }
28 printf("%.5lf",l);
29 }