描述

在一张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. 【C/C++开发】malloc,calloc和realloc的区别和注意事项

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.        内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量. <2&g ...

  2. shrio学习笔记

    Thymeleaf扩展坐标 <!--thyemleaf对shrio的扩展坐标--> <dependency> <groupId>com.github.thebora ...

  3. [.Net] - 生成短 Guid 标识符的方法

    产生字符串(例:49f949d735f5c79e) private string GenerateId() { ; foreach (byte b in Guid.NewGuid().ToByteAr ...

  4. Mybatis笔记4

    mybatis中多对多的步骤 示例:用户和角色,一个用户可以有多个角色,一个角色可以赋予多个用户 步骤: 建立两张表:用户表,角色表,让用户表和角色表具有多对多的关系,需要使用中间表,中间表中包含两张 ...

  5. Python06之分支和循环1(三目运算符)

    Python 为了使得程序更加简洁而新引入过来的一个三目操作符,顾名思义就是有三个参数. 格式: x if 条件表达式 else y 先判断条件表达式真假,真则取 x 的值,否则取 y 的值. 例如: ...

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

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

  7. DBA职责和任务

    DBA守则在对生产环境进行修改前,一定要进行备份,一定要在测试环境进行测试,否则不要进行轻易的更改一次尽量只做一件事,不要受环境影响 DBA的十大任务1.了解和掌握硬件环境2.规划数据库3.安装数据库 ...

  8. HTML5 Notification

    H5的Notification特性 Web桌面通知 Notification API的通知接口用于向用户配置和显示桌面通知. 生产环境仅支持https下使用:否则会被默认禁止.开发环境可以在local ...

  9. AJAX 调用WebService 、WebApi 增删改查

    WebService 页面: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3 ...

  10. 用 Scoop 管理你的 Windows 软件

    包管理系统,Homebrew 就是 macOS 上体验最佳的软件包管理,能帮助我们方便快捷.干净利落的管理软件.在Windows平台上也有一个非常棒的包管理软件--Scoop.Scoop 最适合安装那 ...