#include<stdlib.h>
#define s 30005
int cmp(const void *a,const void *b){
int *x=(int *)a;
int *y=(int *)b;
return *x-*y;
}
int main()
{
int a[s];
int i,j,dis,n,m,mid;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&m);
for(j=0;j<m;j++)
scanf("%d ",&a[j]);
qsort(a,m,sizeof(int),cmp); //It is sorted to calculate nedian(mid)
j=m/2;
if(m%2==0)
mid=(a[j]+a[j-1])/2;
else
mid=a[j];
dis=0;
for(j=0;j<m;j++)
{
if(mid>a[j])
dis=dis+(mid-a[j]);
else
dis=dis+(a[j]-mid);
}
printf("%d\n",dis);
}
return 0;
}
No comments:
Post a Comment