标签:end iss center ott tom ane for otto 简易
2 2 2 4 3 2 4 6
2 4思路:排序后,计算中间位置到其他各点的距离奇数时候就一个中间点偶数时候两个中间点距离的最小值是答案
#include <iostream> using namespace std; long a[10001]; void sort(int n) { for (int i = n; i >=1; i--) { for (int j = 1; j < i; j++) { if (a[j]>a[j + 1]) { long t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } } long myDistance(int mid,int n) { long dis = 0; for (int i = 1; i <= mid; i++) { dis += a[mid] - a[i]; } for (int i = mid + 1; i <= n; i++) { dis += a[i] - a[mid]; } return dis; } long min(long num1, long num2) { return num1 > num2 ? num2 : num1; } int main() { int T; int n; int mid; cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(n); mid = n / 2; if (n % 2 == 0) { cout << min(myDistance(mid, n), myDistance(mid + 1, n)) << endl; } else { cout << myDistance(mid+1, n) << endl; } } return 0; }
标签:end iss center ott tom ane for otto 简易
原文地址:http://www.cnblogs.com/theskulls/p/6192609.html