题目背景
听说最近石油危机
所以想到了这题
题目描述
某石油公司计划建造一条由东向西的主要输油管道。该管道要穿过一个有n口油井的油田。从每口油井都要有一条输油管道沿最短路径(或南或北)与主管道相连。如果给定n口油井的位置,及它们的x坐标(东西向)和y坐标(南北向),应如何确定主管道的最优位置,即使各油井到主管道之间的输油管道长度总和最小的位置?证明可规定时间内确定主管道的最优位置。
输入格式
第一行是油井数n(1<=n<=10000)
接下来n行是油井的位置,每行2个整数x和y(-10000<=x,y<=10000)
输出格式
只有一行
是油井到主管道之间的输油管道最小长度总和
输入输出样例
输入 #1
1 2
2 2
1 3
3 -2
3 3
输出 #
6
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
#include<math.h>
#include<climits>
#include <map>
using namespace std;
int n,m,t,ans = 0;
int a[1000000];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++) {
int x;
cin >> x >> a[i];
}
sort(a+1,a+1+n);
t = n/2;
for (int i = 1; i <= t; i++) {
ans+=a[n-i+1] - a[i];
}
cout << ans;
return 0;
}