-
Notifications
You must be signed in to change notification settings - Fork 0
/
10041.cpp
47 lines (38 loc) · 843 Bytes
/
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
#include <cmath>
#include <iostream>
using namespace std;
void sortS(int *arr, int len)
{
int i = 0, j = len - 1, tmpInt;
int pivot = arr[len / 2];
if (len < 2) return;
while (true) {
while (arr[i] < pivot)
++i;
while (arr[j] > pivot)
--j;
if (i >= j) break;
tmpInt = arr[i];
arr[i++] = arr[j];
arr[j--] = tmpInt;
}
sortS(arr, i);
sortS(arr + i, len - i);
}
int main(void)
{
int times, ALen, arr[30001], i, sum;
cin >> times;
while (times-- > 0) {
cin >> ALen;
for (i = 0; i < ALen; ++i)
cin >> arr[i];
sortS(arr, ALen);
sum = 0;
for (i = 0; i < ALen; ++i) {
sum += abs(arr[i] - arr[ALen / 2]);
}
cout << sum << endl;
}
return 0;
}