-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA10041.cpp
49 lines (49 loc) · 985 Bytes
/
UVA10041.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
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<string>
#include<vector>
#define swap(x, y, z) ((z)=(x), (x)=(y), (y)=(z))
using namespace std;
void sort(vector <int> & in) {
bool f;
int n = in.size() - 1, temp;
for (int i = 0; i < n; ++i) {
f = 0;
for (int j = 0; j < n - i; ++j) {
if (in[j] > in[j + 1]) {
swap(in[j], in[j + 1], temp);
f = 1;
}
}
if (!f) return;
}
}
int main() {
int caseN, n, temp, med, sum;
string buffer;
vector <int> data;
cin >> caseN;
while (caseN--) {
sum = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> temp;
data.push_back(temp);
}
sort(data);
/*cout << "--";
for (int i = 0; i < n; ++i)
cout << data[i] << ',';
cout << "--\n";*/
if (--n % 2)
med = (data[n / 2] + data[(n / 2) + 1]) / 2;
else
med = data[n / 2];
//cout << "n : " << n << " med : " << med << '\n';
for (int i = 0; i <= n; ++i)
sum += (abs(med - data[i]));
cout << sum << '\n';
data.clear();
}
}