-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10041.cpp
67 lines (65 loc) · 1.1 KB
/
10041.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<bitset>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
int partition(int s[], int l, int h)
{
int i; /* counter */
int p; /* pivot element index */
int firsthigh; /* divider position for pivot */
p = h;
firsthigh = l;
for (i=l; i<h; i++)
if (s[i] < s[p])
{
swap(*&s[i],*&s[firsthigh]);
firsthigh ++;
}
swap(*&s[p],*&s[firsthigh]);
return(firsthigh);
}
void quicksort(int s[], int l, int h)
{
int p; /* index of partition */
if ((h-l)>0)
{
p = partition(s,l,h);
quicksort(s,l,p-1);
quicksort(s,p+1,h);
}
}
int main()
{
int t,j;
scanf("%d",&t);
for(j=0;j<t;j++)
{
int n,i,a[1000]={0},m,x;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
m=a[n/2];
long s=0;
for(i=0;i<n;i++)
{
x=m-a[i];
if(x<0)
x=x*(-1);
s=s+x;
}
printf("%ld\n",s);
}
return 0;
}