You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
703 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}