ACDream 1735 输油管道

输油管道

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262144/131072KB (Java/Others)

Problem Description

平面上有n个油井,现在要建立一条主干线,用来把所有的油井产出的原油都输送出去,主干线是平行于x轴的一条直线,每个油井通过一条支线把原油输送到主干线上,现在给定n个油井在平面上的坐标,那么应该把主干线建在什么地方才能让所有的支干线的总长度最小呢?

Input

首先一个正整数n,接下来n行每行两个整数,代表n个油井在平面上的位置。n和坐标都是小于等于1000000的正整数。

Output

输出总的支干线长度的最小值,每个结果占一行。

Sample Input

2
0 0
10 10

Sample Output

10

Manager

 
题意:给出点的坐标,求最小的输油管的长度。
分析:可以转化成x坐标上的一些固定的点,求一个点到这些点的距离最小。可以证明这个点的坐标的是这些点的中位数。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#define LL long long
using namespace std;
const int MAXN=1000000+5;
int a[MAXN];
int main()
{
    int n,temp,x;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d %d",&temp,&a[i]);
    sort(a,a+n);
    if(n%2) x=a[n/2];
    else x=(a[n/2]+a[n/2-1])/2;
    LL ans=0;
    for(int i=0;i<n;i++)
        ans+=(LL)abs(x-a[i]);
    printf("%lld\n",ans);
    return 0;
}
View Code

 

posted @ 2015-05-09 16:19  Cliff Chen  阅读(241)  评论(0编辑  收藏  举报