NC52818. Rolling Variance
描述
输入描述
The input contains at most 30 sets. For each set:
The first line contains 2 integers n, m .
The second line contains n integers ().
输出描述
For each set, (n - m + 1) lines with floating numbers .
Your answer will be considered correct if its absolute or relative error does not exceed .
示例1
输入:
3 2 1 3 2
输出:
1.41421356 0.70710678
示例2
输入:
5 3 1 3 2 4 5
输出:
1.00000000 1.00000000 1.52752523
C++14(g++5.4) 解法, 执行用时: 116ms, 内存消耗: 2144K, 提交时间: 2019-10-06 16:17:26
#include <bits/stdc++.h> using namespace std; #define LL long long #define N 1000100 double A[N],sum1,S; int n,m; int main () { cin>>n>>m; for(int i=1;i<=n;i++) cin>>A[i]; for(int i=1;i<=m;i++) sum1+=A[i]*A[i],S+=A[i]; printf("%.8lf\n",sqrt((sum1-S*S/m)/(m-1))); for(int i=1;i<=n-m;i++) { sum1+=A[i+m]*A[i+m]; sum1-=A[i]*A[i]; S+=A[i+m]; S-=A[i]; printf("%.8lf\n",sqrt((sum1-S*S/m)/(m-1))); } }
C++11(clang++ 3.9) 解法, 执行用时: 97ms, 内存消耗: 3560K, 提交时间: 2020-02-16 22:37:27
#include<cstdio> #include<cmath> using namespace std; int n,m,i; double a[100100],b[100100],c[100100]; double ave,ans; int main() { scanf("%d %d",&n,&m); for(i=1;i<=n;i++) { scanf("%lf",&a[i]); b[i]=b[i-1]+a[i]; c[i]=c[i-1]+a[i]*a[i]; } for(i=m;i<=n;i++) { ave=(b[i]-b[i-m])/m; ans=(c[i]-c[i-m]+ave*ave*m-2*ave*(b[i]-b[i-m]))/(m-1); printf("%.8f\n",sqrt(ans)); } }