'commit'
This commit is contained in:
37
TangDou/CSP-J/AcWingTraining/T5/P1862.cpp
Normal file
37
TangDou/CSP-J/AcWingTraining/T5/P1862.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user