forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10041.cpp
More file actions
49 lines (44 loc) · 666 Bytes
/
10041.cpp
File metadata and controls
49 lines (44 loc) · 666 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define MAXN 30001
int RELATIVE[MAXN];
long long DIFF, MID;
int sf(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
void COM_DIFF(int N)
{
int i;
DIFF = 0;
for (i = 0; i < N; i++)
{
DIFF += abs(RELATIVE[i] - MID);
}
printf("%lld\n", DIFF);
}
int main()
{
int N, i, kase;
scanf("%d", &kase);
while (kase--)
{
scanf("%d", &N);
for (i = 0; i < N; i++)
{
scanf("%d", &RELATIVE[i]);
}
qsort(RELATIVE, N, sizeof(int), sf);
int k = N / 2;
if (N % 2 == 0)
{
MID = (RELATIVE[k - 1] + RELATIVE[k]) / 2;
}
else
{
MID = RELATIVE[k];
}
COM_DIFF(N);
}
return 0;
}