ACDream 1735 输油管道

输油管道

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

Submit Statistic Next Problem

Problem Description

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

Input

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

Output

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

Sample Input

2
0 0
10 10

Sample Output

10

Manager

rihkddd

题意:给出点的坐标,求最小的输油管的长度。

分析:可以转化成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;
}

时间: 2024-07-30 13:27:44

ACDream 1735 输油管道的相关文章

acdream 1735 输油管道 贪心

输油管道 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1735 Description 平面上有n个油井,现在要建立一条主干线,用来把所有的油井产出的原油都输送出去,主干线是平行于x轴的一条直线,每个油井通过一条支线把原油输送到主干线上,现在给定n个油井在平面上的坐标,那么应该把主干线建在什么地方才能让所有的支干线的总长度最小呢? A的某一段完全重合,或者能够经过上下左右平移与折线A的

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

acdream 1738 世风日下的哗啦啦族I

原题链接:http://acdream.info/problem?pid=1738 树套树裸题,如下: 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #define lc root<<1 7 #define rc root<<1|1 8 using std::so

数学 ACdream 1196 KIDx&#39;s Triangle

题目传送门 1 /* 2 这道题花了好长时间AC,思路有,但是表达式少写了括号一直乱码,囧! 3 注意:a==0时要特判:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <cmath> 11 using namespace std; 1

ACdream 之ACfun 题解

A - ACfun Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 题目链接点击打开链接 As a former ACMer, "AC" is a special abbreviated word which can bring much pleasure to me. Sometimes it means

Ubuntu下快速搭建ACdream Online Judge v1.5.3

原文:https://github.com/KIDx/ACdream#%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96%E6%A8%A1%E5%9D%97 安装依赖 $ sudo apt-get update $ sudo apt-get install imagemagick $ sudo apt-get install python-software-properties python g++ make $ sudo apt-get install libcairo2

ACdream 1157 Segments(CDQ分治)

题目链接:http://acdream.info/problem?pid=1157 Problem Description 由3钟类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i (1-base) 删除第i条增加的线段,保证每条插入线段最多插入一次,且这次删除操作一定合法3) Q L R(1 <= L <= R <= 1000000000) 查询目前存在的线段中有多少条线段完全包含[L,R]这个线段,线段X

ACDream - Chasing Girl

先上题目: Chasing Girl Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description YYS 是SCAU_ACM里相当有名气的高富帅, 又因为他曾代表SCAU参加 World Final 而被各路亲朋好友仰慕.但YYS 又有另外一个嗜好,就是泡妞.在大学的时候,他经常去找各个女友玩耍, 但在路上却又整天遇到膜拜他的渣渣

ACDream - Graphs

先上题目: Graphs Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N个点,M条边,问是否存在一个连通子图,子图由原图删掉一些点和边(不删亦可),且叶子数>=4(即度为1的点) Input 多组数据,每组数据N,M(0 <= N <= 10000,0 <= M <= 20000) 接下来M