思路:看了两个小时的题目...就是维护个平方和以及和,然后瞎搞一下就好了
#include<bits\stdc++.h>
using namespace std;
#define LL long long
const int maxn = 50005;
LL s[maxn];
/*void solve(int n,int k)
{
double sum = 0;
for(int i = 1;i<=k;i++)
sum+=s[i];
int l = 1;
int r = k;
for (int i = k;k<=n;k++)
{
double ave = sum/k,t=0;
for (int j = l;j<=r;j++)
t+=(s[j]-ave)*(s[j]-ave);
l++;
r++;
sum = sum - s[l-1]+s[r];
if (i==k || ans>t)
ans = t;
}
printf("%.10lf\n",ans);
}*/
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k;
LL suml=0,sumr=0,ans=-1,t=0;
scanf("%d%d",&n,&k);
for(int i = 1;i<=n;i++)
scanf("%lld",&s[i]);
k = n-k;
if(k==0 || k==1)
{
printf("0.000000000\n");
continue;
}
s[n+1]=0;
sort(s+1,s+1+n);
for(int i = 1;i<=k;i++)
{
suml = suml + s[i];
sumr = sumr + s[i]*s[i];
}
for(int i = k;i<=n;i++)
{
t = sumr*k - suml*suml;
if (i==k || ans>t)
ans=t;
suml = suml -s[i-k+1] +s[i+1];
sumr = sumr -s[i-k+1]*s[i-k+1]+s[i+1]*s[i+1];
}
printf("%.10lf\n",(double)ans/(double)k);
}
}