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

Source

第九届北京化工大学程序设计竞赛

Manager

rihkddd




当n为奇数时,显然当主干线在中间点2点时支线距离和最小,为x1+x2



n为偶数时,主干线放在2点或3点时和放在2点,3点之间是距离和是相同的,所以用奇数的先找中间点的方法在偶数的情况下同样适用。

所以这道题的关键是找出来处于中间的一个点

ps:不要忘了数据范围,用int会wa,要改成long long。

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct node{
long long x,y;
}a[1000000+10];
int cmp(node u,node v)
{
return u.y<v.y;
}
int main()
{
long long n,sum=0,i;
scanf("%lld",&n);
for(i=0;i<n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
long long mid=a[n/2].y;
for(i=0;i<n;i++)
{
sum+=abs(a[i].y-mid);
}
printf("%lld\n",sum);
return 0;
}

ACdream - 1735:输油管道的更多相关文章

  1. acdream 1735 输油管道 贪心

    输油管道 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1735 Description ...

  2. ACdream 1735 输油管道 (排序)

    http://acdream.info/problem?pid=1735 官方题解:http://acdream.info/topic?tid=4246 因为主干线是平行于x轴的直线,那么跟x坐标其实 ...

  3. ACdream 1735 输油管道

    输油管道 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262144/131072KB (Java/Others)   Problem Des ...

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

    题目链接:http://acdream.info/problem?pid=1735 输油管道 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 2 ...

  5. ACdream 1214---矩阵连乘

    ACdream 1214---矩阵连乘 Problem Description You might have noticed that there is the new fashion among r ...

  6. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  7. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  8. acdream.A Very Easy Triangle Counting Game(数学推导)

    A - A Very Easy Triangle Counting Game Time Limit:1000MS     Memory Limit:64000KB     64bit IO Forma ...

  9. acdream.Bet(数学推导)

    Bet Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Status Pra ...

随机推荐

  1. bcompare Linux版 无限试用

    需要root权限.   # mv /usr/bin/bcompare /usr/bin/bcompare.real # cat /usr/bin/bcompare #!/bin/sh rm " ...

  2. Codeforces 197D - Infinite Maze

    197D - Infinite Maze 思路:bfs,如果一个点被搜到第二次,那么就是符合要求的. 用vis[i][j].x,vis[i][j].y表示i,j(i,j是取模过后的值)这个点第一次被搜 ...

  3. Codeforces 447C - DZY Loves Sequences

    447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  4. synchronized同步代码块锁释放

    今天发现自己写的线上程序出现数据库不能同步的问题,查看日志已经停止记录,随后使用jstack查看线程的运行状况,发现有个同步线程锁住了. 以下是jstack -l 637  问题线程的内容. &quo ...

  5. 国际音标en

    元.         辅  

  6. HDU 3226 背包

    转载自:http://www.cppblog.com/dango/archive/2010/08/26/124881.aspx 貌似是01背包的强化版.但是感觉这样写好理解些.就是01背包拓展了.

  7. net负载均衡服务器架构网址

    https://blog.csdn.net/orichisonic/article/details/71122291 https://blog.csdn.net/huangxiangec/articl ...

  8. bzoj1619

    题解: 简单灌水 从最高的开始 代码: #include<bits/stdc++.h> ; typedef long long ll; using namespace std; ]={,, ...

  9. 通过Viewpager 来实现微信界面左右滑动。

    package com.lixu.huadong; import java.util.ArrayList; import android.os.Bundle; import android.suppo ...

  10. Mac自带的本地服务器的使用

    1. 打开终端,开启Apache: //开启apache: sudo apachectl start //重启apache: sudo apachectl restart //关闭apache: su ...