描述

在一张2D地图上有N座城市,坐标依次是(X1, Y1), (X2, Y2), ... (XN, YN)。

现在H国要修建一条平行于X轴的天然气主管道。这条管道非常长,可以认为是一条平行于X轴的直线。

小Ho想知道如何修建这条管道,可以使N座城市到管道的垂直距离之和最小。请你求出这个最小的距离之和。

输入

第一行包含一个整数N。

以下N行每行包含两个整数Xi, Yi。

1 <= N <= 100000

0 <= Xi, Yi <= 1000000

输出

一个整数,代表最小的距离之和。

样例输入

4
0 0
0 100
100 0
100 100

样例输出

200

直接排序在中点位置,对于偶数情况,中间两个点都可以,答案相同,因为要求是整数。也可以考虑每次求一个最大y和最小y之间的差,都加起来,因为管道肯定在他俩之间。
代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
int a[];
int main() {
int n,x;
ll sum = ;
scanf("%d",&n);
for(int i = ;i < n;i ++) {
scanf("%d%d",&x,&a[i]);
}
sort(a,a + n);
for(int i = ;i < n;i ++) {
sum += abs(a[i] - a[n / ]);
}
printf("%lld",sum);
}

hihocoder 1931 最短管道距离的更多相关文章

  1. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  2. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  3. LeetCode 243. Shortest Word Distance (最短单词距离)$

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  4. [Swift]LeetCode244.最短单词距离 II $ Shortest Word Distance II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  5. [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  6. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  7. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  9. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

随机推荐

  1. 030 Android 第三方开源下拉框:NiceSpinner的使用+自定义Button样式+shape绘制控件背景图+图片选择器(selector)

    1.NiceSpinner下拉框控件介绍 Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框 ...

  2. git config命令详解

     Git有一个工具被称为git config,它允许你获得和设置配置变量:这些变量可以控制Git的外观和操作的各个方面. 一. 配置文件的存储位置 这些变量可以被存储在三个不同的位置: 1./etc/ ...

  3. Java 发送http GET/POST请求

    最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途.轻量极的HTTP客户端,使 ...

  4. CSS中的position和float

    对基础知识再度做个巩固和梳理. 一.position定位 (一):position的属性 1.absolute:生成绝对定位的元素,相对于最近一级定位不是static的父元素来进行定位: 2.rela ...

  5. Linux和Windows系统目录结构区别

    Windows目录结构图 Linux目录结构图 我们所有的操作尽量都要在/home/username目录下进行. 快捷进入家目录方式是cd ~.

  6. Python之推导式笔记

    观察下面的代码: list1 = [] for i in range(10): list1.append(i) print(list1) 作为一个Java出身的程序员,我一定会这么写代码去生成一个列表 ...

  7. Lucene入门+实现

    Lucene简介详情见:(https://blog.csdn.net/Regan_Hoo/article/details/78802897) lucene实现原理 其实网上很多资料表明了,lucene ...

  8. vue 的 solt 子组件过滤

    如上图: 1.定义了一个类似下拉的组件 mySelect , 然后里面有自定义的组件 myOptions 2.有很多时候,我们希望, mySelect 组件内部的子组件,只能是 myOptions . ...

  9. Z算法板子

    给定一个串$s$, $Z$算法可以$O(n)$时间求出一个$z$数组 $z_i$表示$s[i...n]$与$s$的前缀匹配的最长长度, 下标从$0$开始 void init(char *s, int ...

  10. javascript -- 时间转换

    function numFormat(num){ //时间处理 return ('00' + num).substr(-2);    #处理 日期前面有0的情况}function timeFormat ...