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; }
相关文章推荐
- UVA 10041 Vito's Family (中位数)
- UVA 10041 Vito's Family 求中位数 模拟
- UVA - 10041 Vito's Family (中位数)
- UVA - 10041 Vito's Family 求中位数
- UVa 10041 Vito's Family (中位数&快速选择)
- uva 10041 Vito's Family(检索)
- UVa Problem Solution: 10041 - Vito's Family
- uva10041 - Vito's Family(求中位数)
- uva - 10041 - Vito's Family(数学)
- UVa 10041 - Vito's Family
- UVa Problem 10041 Vito’s Family (Vito 家族)
- uva 10041 Vito's Family_贪心
- uva10041-Vito‘s family(全部枚举)
- UVA - 10041 Vito's Family(贪心)
- UVa 10041 - Vito's Family
- UVa 10041 - Vito's Family
- uva 10041 Vito's Family
- uva10041 Vito's Family
- uva 10041 Vito's family
- Uva 10041 Vito's Family