标签:des style blog http color io os ar java
贪心:
保存连续的n-k个数,求最小的一段方差。。。。预处理O1算期望。。。
2 3 2 -1 0 1 4 2 -2 -1 1 2
0 0.5
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=50050; double sum[maxn],sum2[maxn]; int a[maxn]; int n,k; double FC(int from,int to) { double n=to-from+1.; double V=(sum[to]-sum[from-1])/n; double A=sum2[to]-sum2[from-1]; double B=sum[to]-sum[from-1]; return A-2*V*B+n*V*V; } int main() { int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",a+i); } if(k==n||k==n-1) { puts("0.0000000000"); continue; } sort(a+1,a+1+n); for(int i=1;i<=n;i++) { sum[i]=sum[i-1]+1.*a[i]; sum2[i]=sum2[i-1]+1.*a[i]*a[i]; } int m=n-k; double ans=1e30; for(int i=1;i+m-1<=n;i++) { int j=i+m-1; ans=min(ans,FC(i,j)); } printf("%.10lf\n",ans); } return 0; }
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/ck_boss/article/details/40397145