Teemo likes to drink raspberry juice.  He even spent some of his spare time tomake the raspberry juice himself. The way to make the raspberries juice is simple. You just have to press the raspberries through a fine sieve.

Unfortunately,today Teemo was splited in several pieces by the sieve which was used to makethe raspberry juice. The pieces were losted in the huge two-dimensional map. Onlywhen all the pieces gather, can Teemo drink the raspberry juice he made today.

Teemo's piece can only move parallel to the x or y axis, or he would be hated by theraspberry Queen and will not be able to have raspberry juice any more. One of the piece of Teemo should carry the raspberry juice.In order to avoid spilling, this piece cannot move anymore.

Teemo’spiece are lazy, they’d like to make the sum of paths be the minimal. Your task is to find the minimal sum of the paths.

InputFormat
The first line contains a integer n (1<=n<=100000) represent the number of thepieces. Then next n lines. Each line contains the pairs of xi, yi(-1000000000<xi,yi<1000000000) in turn as points by order.

OutputFormat
Printa single line contains the minimal sum of the paths.

样例输入1

3
1 0
2 0
3 0

样例输出1

2

样例输入2

5
4 1
4 4
9 2
2 9
2 6

样例输出2

21

//必须要先排序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <map>
#include <vector>
#include <stack>
using namespace std;
#define ll long long
#define N 100009
#define M 5000000000000009
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ph push_back
#define mod 1000000007
struct Node{
ll id,x,y;
}nod[N];
ll sumx[N],sumy[N],l[N],r[N];
bool cmpx(Node a,Node b){
return a.x<b.x;
}
bool cmpy(Node a,Node b)
{
return a.y<b.y;
}
ll n;
int main()
{
scanf("%lld",&n);
gep1(i,,n) {
scanf("%lld%lld",&nod[i].x,&nod[i].y);
nod[i].id=i;
}
sort(nod+,nod++n,cmpx);
gep(i,,n){
sumx[i]=sumx[i-]+nod[i].x;
}
ll sx=;
// 1 2 3 4 5 (x)
// 3 3-1+3-2 + 4-3+5-3
gep(i,,n){
sx=nod[i].x*(i-)-sumx[i-]+sumx[n]-sumx[i]-nod[i].x*(n-i);
l[nod[i].id]=sx;
}
sort(nod+,nod++n,cmpy);
gep(i,,n){
sumy[i]=sumy[i-]+nod[i].y;
}
ll sy=;
gep(i,,n){
sy=nod[i].y*(i-)-sumy[i-]+sumy[n]-sumy[i]-nod[i].y*(n-i);
r[nod[i].id]=sy;//每一次的排序都会造成id的变化,id :最初那个数据在当前的位置
}
ll MIN=M;
gep(i,,n){
MIN=min(MIN,l[i]+r[i]);
}
printf("%lld\n",MIN);
return ;
}

ACM训练联盟周赛 K. Teemo's reunited的更多相关文章

  1. ACM训练联盟周赛 A. Teemo's bad day

    65536K   Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework ...

  2. ACM训练联盟周赛 G. Teemo's convex polygon

    65536K   Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo co ...

  3. 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)

    ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...

  4. 计蒜客 ACM训练联盟周赛 第一场 Christina式方格取数 思维

    助手Christina发明了一种方格取数的新玩法:在n*m的方格棋盘里,每个格子里写一个数.两个人轮流给格子染色,直到所有格子都染了色.在所有格子染色完后,计算双方的分数.对于任意两个相邻(即有公共边 ...

  5. 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂

    题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...

  6. 计蒜客 ACM训练联盟周赛 第一场 从零开始的神棍之路 暴力dfs

    题目描述 ggwdwsbs最近被Zeratul和Kyurem拉入了日本麻将的坑.现在,ggwdwsbs有13张牌,Kyurem又打了一张,加起来有14张牌.ggwdwsbs想拜托你帮他判断一下,这14 ...

  7. ACM训练联盟周赛(第三场)

    A.Teemo's bad day Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his ...

  8. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...

  9. ACM训练联盟周赛 Teemo's formula

    Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...

随机推荐

  1. [已读]Nodejs高级编程

    封面太让人想吐槽了,真的很像<javascript高级程序设计>有木有 内容我觉得还不错,流畅,见过的nodejs书籍中最详细的一本.很多书会把express及使用案例作为重点,但是它不是 ...

  2. Chess 模拟

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6114 Problem Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋 ...

  3. Apache——DBUtils框架ResultSetHandler接口使用

    参考链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDB ...

  4. Vue.js - Day5 - Webpack

    在网页中会引用哪些常见的静态资源? JS .js .jsx .coffee .ts(TypeScript 类 C# 语言) CSS .css .less .sass .scss Images .jpg ...

  5. AJAX的JSON方式传回方法

    AJAX返回数据的类型有两种,一种是TEXT类型,一种是JSON类型. 使用TEXT类型,访问数据库后将结果拼接成字符串,返回时在拆分成数组使用. JSON直接将结果转成JSON数据,返回时直接使用. ...

  6. TFS数据库分离附加经验总结

    因TFS数据库已经100多G,所在的服务器D盘已没有空间满足tfs数据库的增长速度,故必须分离复制到其它盘.在分离过程中,先后分离了ReportServer.ReportServerTempDB.Tf ...

  7. IDEA 启用/禁用 Run Dashboard

    一.启用 方式一: 创建/打开一个SpringBoot项目[或者点击Run --> Edit Configurations 添加 Spring Boot 类型的项目配置:或者如图在红框处添加配置 ...

  8. URAL 2047 Maths (数学)

    对于一个数来说,它的除数是确定的,那么它的前驱也是确定的,而起点只能是1或2,所以只要类似筛法先预处理出每个数的除数个数 ,然后递推出每个数往前的延伸的链长,更新最大长度,记录对应数字.找到maxn以 ...

  9. 贴一发STL源码

    int my_lower_bound(int size, long long key){    int first = 0, middle;    int half, len;    len = si ...

  10. leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

    1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...