Title Link: Http://codeforces.com/contest/808/problem/B
Test instructions: The number of N, and the average number of each k.
The ghost knows how I can come up with a tree-like array of rock music.
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong LongLL;5 Const intMAXN =200200;6 intN, K;7 LL A[MAXN], BIT[MAXN];8 intLowbit (intx) {returnx& (-x);}9 voidAddintId,ll val) { for(intI=id;i<=k;i+=lowbit (i)) bit[i]+=Val;}Ten voidSumintID, ll& ret) { for(intI=id;i>0; I-=lowbit (i)) ret+=bit[i];} One A intMain () { - //freopen ("in", "R", stdin); - while(~SCANF ("%d%d",&n,&k)) { theLL ret =0; -memset (bit,0,sizeof(bit)); - for(inti =1; I <= N; i++) scanf ("%i64d", &a[i]); - for(inti =1; I <= K; i++) Add (I,a[i]); + sum (k, ret); - for(inti = k +1; I <= N; i++) { +Add (i%k+1,-a[i-K]); AAdd (i%k+1, A[i]); at sum (k, ret); - } -printf"%.10f\n", (Double) ret/(n-k+1)); - } - return 0; -}
Directly with the prefix and maintenance engage.
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong LongLL;5 Const intMAXN =200200;6 intN, K;7 LL A[MAXN], S[MAXN];8 9 intMain () {Ten //freopen ("in", "R", stdin); One while(~SCANF ("%d%d",&n,&k)) { Amemset (s),0,sizeof(s)); - for(inti =1; I <= N; i++) { -scanf"%i64d", &a[i]); theS[i] = s[i-1] +A[i]; - } -LL ret =.0; - for(inti = k; I <= N; i++) ret + = S[i]-s[i-K]; +printf"%.10f\n", (Double) RET/(N-k +1)); - } + return 0; A}
[cf808b] Average Sleep time ([Force] tree array, math)