题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x+y, x-y)). 转成Manhattan distance后排序前缀和维护即可.

--------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
#define X(o) x[rx[o]]
#define Y(o) y[ry[o]]
 
typedef long long ll;
 
const int maxn = 100009;
 
ll smx[maxn], smy[maxn], ans;
int N, x[maxn], y[maxn], rx[maxn], ry[maxn];
 
bool cmpX(const int &l, const int &r) {
return x[l] < x[r];
}
 
bool cmpY(const int &l, const int &r) {
return y[l] < y[r];
}
 
int BS(int c[], int r[], int v) {
int L = 0, R = N - 1;
while(L <= R) {
int m = (L + R) >> 1;
if(c[r[m]] == v)
return m;
c[r[m]] < v ? L = m + 1 : R = m - 1;
}
return -1;
}
 
int main() {
scanf("%d", &N);
for(int i = 0; i < N; i++) {
rx[i] = ry[i] = i;
int _x, _y;
scanf("%d%d", &_x, &_y);
x[i] = _x + _y;
y[i] = _x - _y;
}
sort(rx, rx + N, cmpX);
sort(ry, ry + N, cmpY);
smx[0] = X(0);
smy[0] = Y(0);
for(int i = 1; i < N; i++) {
smx[i] = smx[i - 1] + X(i);
smy[i] = smy[i - 1] + Y(i);
}
ans = 1LL << 60;
for(int i = 0; i < N; i++) {
int px = BS(x, rx, x[i]), py = BS(y, ry, y[i]);
ll t = 0;
if(!px) {
t += smx[N - 1] - ll(N) * x[i];
} else {
t += ll(x[i]) * (px * 2 + 2 - N) - 2LL * smx[px] + smx[N - 1];
}
if(!py) {
t += smy[N - 1] - ll(N) * y[i];
} else {
t += ll(y[i]) * (py * 2 + 2 - N) - 2LL * smy[py] + smy[N - 1];
}
ans = min(t, ans);
}
printf("%lld\n", ans >> 1);
return 0;
}

-------------------------------------------------------------------------

3170: [Tjoi 2013]松鼠聚会

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 874  Solved: 421
[Submit][Status][Discuss]

Description

有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1。现在N个松鼠要走到一个松鼠家去,求走过的最短距离。

Input

第一行给出数字N,表示有多少只小松鼠。0<=N<=10^5
下面N行,每行给出x,y表示其家的坐标。
-10^9<=x,y<=10^9

Output

表示为了聚会走的路程和最小为多少。

Sample Input

6
-4 -1
-1 -2
2 -4
0 2
0 3
5 -2

Sample Output

20

HINT

Source

BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )的更多相关文章

  1. BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  2. Bzoj 3170[Tjoi 2013]松鼠聚会 曼哈顿距离与切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  3. bzoj 3170: [Tjoi 2013]松鼠聚会

    #include<cstdio> #include<iostream> #include<algorithm> #define M 100008 using nam ...

  4. bzoj 3170 Tjoi 2013 松鼠聚会 曼哈顿距离&&切比雪夫距离

    因为曼哈顿距离很好求,所以要把每个点的坐标转换一下. 转自:http://blog.csdn.net/slongle_amazing/article/details/50911504 题解 两个点的切 ...

  5. bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)

    题目链接: 3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MB Description 有N个小松鼠,它们的家用一个点x,y表 ...

  6. 3170: [Tjoi 2013]松鼠聚会

    题目大意 给定n个点,找到一个点使这个点到其他所有点的切比雪夫距离之和最小. 题解 我们知道切比雪夫距离和曼哈顿距离的转化公式 \(1\)表示切比雪夫距离,\(2\)表示曼哈顿距离 我们有: \(x_ ...

  7. BZOJ3170: [Tjoi 2013]松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 531  Solved: 249[Submit][Statu ...

  8. [Tjoi 2013]松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  9. 【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系

    题目描述 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...

随机推荐

  1. Flash,一次Bug的思考

    我绝对不算是F黑,大部分时候,我还是很挺Flash平台的,Flash提供了很好的跨平台特性以及Flash Player11后的GPU加速.Stage3D等等,对于开发者来说,绝对让人欣喜若狂(对我是这 ...

  2. dog-fooding-our-api-authentication

    Dog-fooding our API - Authentication http://blog.mirajavora.com/authenticate-web-api-using-access-to ...

  3. Setup Factory

    Setup Factory   生成安装程序

  4. ORACLE EBS R12 FOR LINUX 开机后如何启动数据库、应用脚本[Z]

    在Linux中安裝EBS  R12後, EBS關閉與啟動的程序為: 1. 關閉EBS - 先關閉Applications Server $ cd /d01/oracle/VIS/inst/apps/V ...

  5. Spring IOC的描述和Spring的注解(转)

    Spring常用的注解 本文系转载:转载网址: http://www.cnblogs.com/xdp-gacl/p/3495887.html http://ljhzzyx.blog.163.com/b ...

  6. C语言实现有序二叉树(1)

    在cpp中使用的C语言 头文件 /* 有序二叉树 BsTree */ #ifndef _BT_H #define _BT_H /*节点*/ typedef struct BsTreeNode { in ...

  7. google base之IncomingTaskQueue

    如同名称描述的那样,这个类就是个taskqueue,也就是任务队列,添加任务到队列,然后由MessageLoop去执行task,比较关心的函数如下: bool IncomingTaskQueue::A ...

  8. android应用程序的组成部分

    android 应用程序的组成部分 activity 应用表示层,应用程序中每一个UI都是通过activity类或者多个扩展实现的.activity使用fragment和视图来布局和显示信息,以及响应 ...

  9. spoj COT2 - Count on a tree II 树上莫队

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  10. Delphi OleVariant 类型的用法

    因客户需求,对客户的指纹机与公司产品进行集成,需要对指纹机做接口的二次开发,郁闷的是产商只提供了VB和C的DEMO示例,没有Delphi的,公司没有VB,C的环境,不能打开这二种语言的示例,因为本公司 ...