UVa10041

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=982

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

int main(){
int r;
cin >> r;
int n;
vector<int> v;
while(r--){
v.clear();
int tmp;
cin >> n;
for(int i=0; i<n; ++i){
cin >> tmp;
v.push_back(tmp);
}
sort(v.begin(), v.end());
int idx = v.size()/2;
int total = 0;
for(int i=0; i<v.size(); ++i){
total += abs(v[idx] - v[i]);
}
cout << total << '\n';
}
return 0;
}