您的位置:首页 > 其它

UVa 10041 Vito's Family (median selection)

2010-03-31 21:09 337 查看
/*
File: 10041.cpp
Author: ACboy
Date: 2010-3-31
Result: AC
Descripition: UVa 10041 Vito's Family (median selection)
*/

#include <iostream>
#include <algorithm>
using namespace std;

int data[30100];

int get_distance(int pos, int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res += abs(pos - data[i]);
}
return res;
}

int main()
{
int n;
#ifndef ONLINE_JUDGE
freopen("10041.txt", "r", stdin);
#endif
cin >> n;

while (n--)
{
int i;
int count;
cin >> count;
int total = 0;
for (i = 0; i < count; i++) {
cin >> data[i];
total += data[i];
}
sort(data, data + count);
int pos;
int result;
if (count % 2 == 0) {
pos = (data[count / 2] + data[count / 2 - 1]) / 2;
result = get_distance(pos, count);
} else {
pos = data[count / 2];
result = get_distance(pos, count);
}
cout << result << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  distance file 2010