38 lines
703 B
C++
38 lines
703 B
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
using namespace std;
|
|||
|
|
|
|||
|
|
const int N = 10005;
|
|||
|
|
/*
|
|||
|
|
文件名:petroleum
|
|||
|
|
题目:洛谷 P1862 输油管道问题
|
|||
|
|
题目网址:https://www.luogu.com.cn/problem/P1862
|
|||
|
|
|
|||
|
|
输入:
|
|||
|
|
5
|
|||
|
|
1 2
|
|||
|
|
2 2
|
|||
|
|
1 3
|
|||
|
|
3 -2
|
|||
|
|
3 3
|
|||
|
|
输出:
|
|||
|
|
6
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
int n, a[N]; // a[0..n-1]。第i口油井的纵坐标在a[i]
|
|||
|
|
|
|||
|
|
int main() {
|
|||
|
|
// 输入数据
|
|||
|
|
cin >> n;
|
|||
|
|
for (int i = 0; i < n; i++) cin >> a[i] >> a[i]; // 横坐标说没有用的,可以直接抛弃掉
|
|||
|
|
|
|||
|
|
// 排序
|
|||
|
|
sort(a, a + n);
|
|||
|
|
|
|||
|
|
// 两端对称取长短。中位数原理。
|
|||
|
|
int sum = 0;
|
|||
|
|
for (int i = 0, j = n - 1; i <= j; i++, j--) sum += a[j] - a[i];
|
|||
|
|
|
|||
|
|
cout << sum << endl;
|
|||
|
|
return 0;
|
|||
|
|
}
|