acoj-1735 输油管道 【中位数】

题目链接:http://acdream.info/problem?pid=1735

输油管道

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

Problem Description

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

acoj-1735 输油管道          【中位数】

Input

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

Output

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

Sample Input

2
0 0
10 10

Sample Output

10

解题思路:取y值中位数,总干线建到中位数那口井上,总支干线长度和最小。这道题目求中位数,不需要排序,可以利用快排原理进行部分搜取就可以求出中位数了。
代码:
 #include <fstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib> using namespace std; const int nn=;
int a[nn],n,n2,ans;
long long cnt; int quick_partition(int i,int j);
void quick_sort(int s,int t); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
while(~scanf("%d",&n)){
n2=(n+)>>;
for(int i=;i<=n;i++) scanf("%*d%d",&a[i]);
quick_sort(,n);
cnt=;
for(int i=;i<=n;i++) cnt+=(long long)abs(a[i]-ans);
printf("%lld\n",cnt);
}
return ;
}
int quick_partition(int i,int j){
a[]=a[i];
while(i<j){
while(i<j&&a[j]>=a[]) j--;
if(i<j) a[i]=a[j],i++;
while(i<j&&a[i]<=a[]) i++;
if(i<j) a[j]=a[i],j--;
}
a[i]=a[];
return i;
}
void quick_sort(int s,int t){
if(s<t){
int tmp=quick_partition(s,t);
if(tmp==n2){
ans=a[tmp];
}else if(tmp<n2){
quick_sort(tmp+,t);
}else{
quick_sort(s,tmp-);
}
}else if(s==t)
ans=a[s];
}
上一篇:LwIP Application Developers Manual11---Initializing lwIP


下一篇:centos7部署phpipam(ip管理系统)