描述

在一张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. docker里安装kali linux

    docker里安装kali linux 官网镜像 docker search kali docker pull kalilinux/kali-linux-docker vi /etc/apt/sour ...

  2. Django 之redis的应用

    redis概述 redis是一种nosql数据库,他的数据是保存在内存中,同时redis可以定时把内存数据同步到磁盘,即可以将数据持久化,并且他比memcached支持更多的数据结构(string,l ...

  3. linux本地内核提权之CVE-2019-13272(鸡肋)

    CVE-2019-13272 发布时间: 2019月7月17日 影响内核版本: Linux Kernel < 5.1.17 漏洞描述: 译文 kernel 5.1.17之前版本中存在安全漏洞,该 ...

  4. 025 Android 带进度条的对话框(ProgressDialog)

    1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...

  5. [转帖]QC 和 PD:关于你所不知道的快充

    QC 和 PD:关于你所不知道的快充 http://www.sohu.com/a/276214250_465976 2018-11-18 06:02 当我们使用支持 PD 或者 QC 快充协议的电源适 ...

  6. todo---git 生成密钥 原理分析

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRJkDZ2z7syFC2QDCaORKF41ecwbL/kyFwkycOVE3MavTRBliAhoAhOaZQTr4j ...

  7. java当中请给出一个oracle的helloworld例子

    [学习笔记] 2.oracle的helloworld例子: import java.sql.*;public class OracleHello{    public static void main ...

  8. Linux shell 中 & && [] [[]] () [] 含义

    | 语法:command 1 | command 2 功能:把第一个命令 command 1 执行的结果作为 command 2 的输入传给 command 2 & & 放在启动参数后 ...

  9. Python--递归函数实现:多维嵌套字典数据无限遍历

    原创:多层嵌套字典无限遍历,实现当value值以特殊字符$开头,并且等于某项值时,用随机函数替换该参数 """处理前的字典{'patient': {'avatarPic' ...

  10. go 实现简单的http web服务

    package main import ( "fmt" "net/http" ) func hello(w http.ResponseWriter, r *ht ...