2016长城信息杯中国大学生程序设计竞赛中南邀请赛 xtu 1249 Rolling Variance

http://www.dengwenhuo.cn/?id=195

Bobo learnt that the variance of a sequencea1,a2,,anis

ni=1(aia¯)2n1

where

a¯=ni=1ain.


Bobo has a sequencea1,a2,,an, and he would like to find the variance of each consecutive subsequences of lengthm. Formally, thei-th (1inm+1) rolling varianceriis the variance of sequence{ai,ai+1,,ai+m1}.

Input

The input contains at most30sets. For each set:

The first line contains2integersn,m(2mn105).

The second line containsnintegersa1,a2,,an(|ai|100).

Output

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

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

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

(a-b)^2=a^2-2ab+b^2拆开后有

a1^2+a2^2+a3^2+....-2*ave*(a1+a2+a3+.......)+n*ave^2

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
 
int main()
{
    int n,m;
    double ave;
    int sum[100005],a[100005],pf[100005];
    pf[0]=0;
    while(~scanf("%d %d",&n,&m))
    {
        scanf("%d",&a[1]);
        pf[1]=sum[1]=a[1];
        pf[1]*=pf[1];
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
            pf[i]=pf[i-1]+a[i]*a[i];
        }
        for(int i=m;i<=n;i++)
        {
            ave=1.0*(sum[i]-sum[i-m])/m;
           // printf("%d %d %lf\n",pf[i]-pf[i-m],sum[i]-sum[i-m],m*ave*ave);
            printf("%.8lf\n",sqrt(  (pf[i]-pf[i-m]-2*ave*(sum[i]-sum[i-m] )+m*ave*ave) /(m-1) ));
        }
    }
    return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/A852852852852/article/details/51720157
个人分类: c/c++ 最爱水题
上一篇2016长城信息杯中国大学生程序设计竞赛中南邀请赛 xtu 1250 Super Fast Fourier Transform
下一篇2016长城信息杯中国大学生程序设计竞赛中南邀请赛 xtu 1243 2016
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭