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. python入门之生成器

    生成器 通过列表生成式,可以直接创建一个列表,但是,受到内存限制,列表容量肯定是有限得.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果仅仅需要访问前面几个元素,那后面绝大多数元素 ...

  2. 十四个关于ASP.NET基础知识问答(C#版)

    这是一些ASP.NET很基础的东西,希望对ASP.NET爱好者特别是刚刚入门的朋友有所帮助虽然示例代码是C#.NET,但是不影响VB.NET朋友的参考.好,继续往下看吧! 1.ASP.NET能在那些系 ...

  3. {ubuntu}不能挂载windows

    sudo apt-get install ntfs-3g sudo ntfsfix /dev/sda?

  4. 解决更新到os x10.11后openssl头文件无法找到的问题

    os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...

  5. Redis性能优化之redis.cnf配置参数

    redis调优总结 1.相应的参数调优 加内存2.redis使用结构调优3.使用合理的数据类型说明:redis存储的数据为redis hash(字符映射表) 单key多字段结构. 1)调整配置文件中配 ...

  6. sql server 2008怎么设置不允许windows身份验证

  7. codevs 1131 统计单词数 2011年NOIP全国联赛普及组

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位 ...

  8. -[UPAInitViewController startAPPay] in libUPAPayPlugin.a(UPAInitViewController.o)

    问题 Undefined symbols for architecture arm64: "_PKPaymentNetworkChinaUnionPay", referenced ...

  9. 背包问题2 (lintcode)

    这里: for(int j = 1;j <= m;j++) result[0][j] = 0x80000000; 不能从0开始,result[0][0]是可以取到的,是0.其他情况取不到才用最小 ...

  10. 洛谷 p1141 01迷宫题解

    很长时间没发博客了,今天水一下 很多dalao说染色(普通的)过不了, 我怎么就过了 其实我也是今天才知道什么是染色(由@你听风在吼 dalao指导) 然后自己打了一个,也不知道叫不叫染色,反正是过了 ...