输油管道(北京化工网赛)

J - 输油管道

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

Problem Description

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

Input

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

Output

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

Sample Input

2
0 0
10 10

Sample Output

10
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 using namespace std;
 5 struct node
 6 {
 7     int x,y;
 8 }a[1000000];
 9 int cmp(node a,node b)
10 {
11     return a.y<b.y;
12 }
13 int main()
14 {
15     int n,i,j;
16     while(cin>>n)
17     {
18         long long sum=0;
19         for(i=0;i<n;i++)
20             scanf("%d%d",&a[i].x,&a[i].y);
21         sort(a,a+n,cmp);
22         if(n%2==0)//偶数个
23         {
24             for(i=0,j=n-1;i<n/2;i++,j--)
25                 sum+=(a[j].y-a[i].y);
26         }
27         else
28         {
29             for(i=0;i<n;i++)
30                 sum+=a[i].y-a[n/2].y;
31         }
32         printf("%lld\n",sum);
33         
34     }
35 }

 

posted @ 2015-07-29 20:18  御心飞行  阅读(191)  评论(0编辑  收藏  举报