hdu5073 最小方差+预处理

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 #define eps 1e-9
 6 long long sum1[50005],sum2[50005];
 7 int a[50005];
 8 int main()
 9 {
10     int T,i,n,k;
11     double pre,now;
12     scanf("%d",&T);
13     while (T--)
14    {
15        scanf("%d%d",&n,&k);
16        for (i=1;i<=n;i++) scanf("%d",&a[i]);
17        sort(a+1,a+n+1);
18        sum1[0]=sum2[0]=0;
19        for (i=1;i<=n;i++)
20        {
21            sum1[i]=sum1[i-1]+a[i];
22            sum2[i]=sum2[i-1]+(long long )a[i]*(long long)a[i];
23        }
24        if (k==n||k==n-1) {printf("0\n"); continue; }
25        k=n-k;
26        pre=(1.0*sum2[k]-1.0*sum1[k]*sum1[k]/(k))/k;
27        for (i=k+1;i<=n;i++)
28        {
29            now=(1.0*(sum2[i]-sum2[i-k])-1.0*(sum1[i]-sum1[i-k])*(sum1[i]-sum1[i-k])/(k))/k;
30            if (pre-now>eps) pre=now;
31        }
32        printf("%lf\n",pre*k);
33    }
34 }

http://acm.hdu.edu.cn/showproblem.php?pid=5073

posted on 2014-10-22 23:09  xiao_xin  阅读(181)  评论(0编辑  收藏  举报

导航