acdream 1735 输油管道 贪心

输油管道

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acdream.info/problem?pid=1735

Description

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

A的某一段完全重合,或者能够经过上下左右平移与折线A的某一段完全重合,则表示秋实大哥吹出了妹子的一部分旋律。

Input

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

Output

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

 

Sample Input

2
0 0
10 10

Sample Output

10

HINT

 

题意

 

题解:

        因为这道题主干线是平行于x轴的一条直线,所以和x坐标无关,自动滤掉x。

        考虑只有两个点的情况,主干线一定在两条直线的中部,这样距离和都是一样的。

        如果再加一个点,中间的那个点一定在直线上。再加一个,直线还是在中间。

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000005
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

struct node
{
    int x,y;
};
bool cmp(node a,node b)
{
    return a.y<b.y;
}
node a[maxn];
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld",&a[i].x,&a[i].y);
    }
    sort(a+1,a+1+n,cmp);
    ll ans=0;
    int tmp=(n+1)/2;
    for(int i=1;i<=n;i++)
    {
        ans+=abs(a[i].y-a[tmp].y);
    }
    cout<<ans<<endl;
}

 

posted @ 2015-05-11 16:33  qscqesze  阅读(421)  评论(0编辑  收藏  举报