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 ;
}

最新文章

  1. Tcl
  2. Xcode插件VVDocumenter Alcatraz KSImageNamed等安装
  3. PyQt4控件失去焦点和获得焦点
  4. RDO部署openstack(1)
  5. Centos5.5下安装cacti
  6. Linux多线程编程和Linux 2.6下的NPTL
  7. Linux 获取文件时间信息 判断文件是否存在
  8. ASP.NET中扩展FileUpload的上传文件的容量
  9. jxl 使用
  10. C++ ofstream和ifstream
  11. Hadoop/Spark开发环境配置
  12. 基于Go的websocket消息服务
  13. rocketmq简单消息发送
  14. Elasticsearch之删除索引
  15. PSO:利用PSO+ω参数实现对一元函数y = sin(10*pi*x) ./ x进行求解优化,找到最优个体适应度—Jason niu
  16. 目标检测(二) SPPNet
  17. Oracle存储过程的调试
  18. Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
  19. 【译】第22节---Fluent API - EntityTypeConfiguration类
  20. 设置https以及http转https的问题

热门文章

  1. Maximum Control (medium) Codeforces - 958B2
  2. sftp 常用命令 以及 以及与 scp 的比较
  3. cucumber 背景和场景的区别
  4. (C#)asp_net调试错误解决方法收集(1)
  5. JSP新闻发布系统
  6. Unity 360 旋转 缩放
  7. [转]SqlServer索引的原理与应用
  8. python搭建ftp服务器
  9. Android Studio项目上传到Jcenter
  10. 你不知道的HTTP之首部字段一览