描述

在一张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. 安装 KVM

    安装 KVM 需要的包 sudo apt-get install -y qemu-kvm qemu-system libvirt-bin virt-manager bridge-utils vlan ...

  2. Django_图片的上传下载显示配置

    图片上传的配置 image = models.ImageField(upload_to='org/%Y/%m',...) upload_to默认是上传到项目的'MEDIA_ROOT/org/%Y/%m ...

  3. Intellij热部署插件JRebel的详细配置及图解

    参考博客地址:https://blog.csdn.net/nyotengu/article/details/80629631 参考博客地址:https://blog.csdn.net/weixin_4 ...

  4. 安装本地 jar 文件

    Maven 提供了 maven-install-plugin 可将 jar 文件安装至本地 repository.安装命令如下: mvn install:install-file -Dfile= -D ...

  5. idea安装svn

    idea不像eclipse那样是用插件,idea是直接指向已经安装好的svn.exe.

  6. [转帖]Docker Hub上镜像发现挖矿蠕虫病毒,已导致2000台主机感染

    Docker Hub上镜像发现挖矿蠕虫病毒,已导致2000台主机感染 https://www.kubernetes.org.cn/5951.html 本来想说可以用 official版本的镜像 但是一 ...

  7. [VS] - 手工打开 WCF 客户端调试工具

    操作步骤 1. 在开始菜单中找到 Visual Studio 命令行工具 2. 输入命令 wcftestclient 即可打开 WCF 客户端测试工具 参考资料http://www.cnblogs.c ...

  8. 使用Docker搭建svn服务器教程

    svn简介 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subv ...

  9. 如何在mongoengine中使用referencefield引用本类

    引用:原文 from mongoengine import * class Employee(Document): name = StringField() boss = ReferenceField ...

  10. dotnet Core 图片验证码

    9102年了,.NET Core 2.x已经稳定,但是还是有很多人搞不定.NET Core的图片验证码. 下面说重点 1.引用Nuget包:System.Drawing.Common 2.像NET F ...