7.货仓选址 绝对值不等式

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100010;
 4 int n;
 5 int a[N];
 6 int main() {
 7     cin >> n;
 8     for (int i = 0; i < n; i++) {
 9         cin >> a[i];
10     }
11     sort(a, a + n);
12     int res = 0;
13     for (int i = 0; i < n; i++) {
14         res += abs(a[i] - a[n / 2]);
15         //a[n / 2]就是中位数
16     }
17     cout << res << endl;
18     return 0;
19 }
原文地址:https://www.cnblogs.com/fx1998/p/13460266.html