hdu 5073

#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int n,k;
long double sumxx[50005];
long double sumx2[50005];
long long v[50005];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
       cin>>n>>k;
       long double sumx=0;
       long double summ=n;
       for(int i=1;i<=n;i++)
       {
           cin>>v[i];
       }
       if(k==n||k==n-1)
       {
           puts("0");continue;
       }
       sort(v+1,v+1+n);
       for(int i=1;i<=n;i++) 
       {
           sumx=sumx+v[i];
           sumxx[i]=sumx;
           sumx2[i]=sumx2[i-1]+v[i]*v[i];
       }
        double anss=sumx2[n]+n*sumxx[n]*sumxx[n]-2*sumxx[n]/n*sumxx[n];
       for(int i=0;i<=k;i++)
       {
           long double tx=sumxx[n-k+i]-sumxx[i];
           long double zxin=tx/(n-k);
           long double tans=sumx2[n-k+i]-sumx2[i]-2*zxin*tx+(n-k)*zxin*zxin;
          if(tans<anss)anss=tans;
       }
        printf("%.12lf\n",anss);
    }
    return 0;
}

posted @ 2015-06-09 16:47  phlsheji  阅读(210)  评论(0编辑  收藏  举报