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 }

 

posted @ 2020-08-21 16:46  kyk333  阅读(141)  评论(0)    收藏  举报