2016年湘潭邀请赛 xtu1249

Rolling Variance

 
Accepted : 73   Submit : 205
Time Limit : 3000 MS   Memory Limit : 65536 KB Special Judge

Rolling Variance

Bobo learnt that the variance of a sequence a1,a2,,an is

ni=1(aia¯)2n1
where
a¯=ni=1ain.

Bobo has a sequence a1,a2,,an,and he would like to find the variance of each consecutive subsequences of length m.Formally, the i-th (1inm+1) rolling variance ri is the variance of sequence {ai,ai+1,,ai+m1}.

Input

The input contains at most 30 sets. For each set:

The first line contains 2 integers n,m (2mn105).

The second line contains n integers a1,a2,,an (|ai|100).

Output

For each set, (nm+1) lines with floating numbers r1,r2,,rnm+1.

Your answer will be considered correct if its absolute or relative error does not exceed 104.

Sample Input

3 2
1 3 2
5 3
1 3 2 4 5

Sample Output

1.41421356
0.70710678
1.00000000
1.00000000
1.52752523


题解:

这个题目其实很简单,但是当年大一还是太年轻了没有做出了,过了一段时间来很快就搞定了,希望再接再厉

就是把分子展开,然后分三类进行计算,使用前缀数组进行维护就可以了,而且都不会超数据


#include
#include
#include
#include
using namespace std;

#define MAXN 100005
int a[MAXN];
int arr[MAXN],sum[MAXN];

int main()
{
    int n,m;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sum[0]=arr[0]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
            arr[i]=arr[i-1]+a[i]*a[i];
        }

        for(int i=m;i<=n;i++){
            double ans=0;
            double temp=(sum[i]-sum[i-m])/(double)m;
            ans+=temp*temp*m+arr[i]-arr[i-m];
            ans-=2*temp*(sum[i]-sum[i-m]);
            printf("%0.8lf\n",sqrt(ans/(m-1)));
        }

    }
    return 0;
}


你可能感兴趣的:(acm)