题目的距离为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. BestCoder Round #14

    Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. linux 命令之 uptime

    uptime 命令是用来查询linux系统负载的. 命令格式 uptime [OPTION] -V 显示版本号 不带參数的 uptime 直接输出系统负载. 何为系统负载呢? 系统平均负载被定义为在特 ...

  3. 2014百度之星第四题Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. JS 匿名函数 自执行

    其实就是将函数直接做为表达调用,使用括号包裹定义函数体,解析器将会以函数表达式的方式去调用定义函数. 常见格式:(function() { /* code */ })(); 解释:包围函数(funct ...

  5. C#核编之System.Environment类

    在前面的例子中用来了Environment.GetCommandLineArgs()这个方法,这个方法就是获取用户的命令行输入,是Environment类的方法之一,该方法的返回值是string[]  ...

  6. datagrid指定行合并导出

    导出代码: public void GridViewToExcel(GridView ctrl, string FileType, string FileName) { HttpContext.Cur ...

  7. vs2010快捷方式

    [窗口快捷键] Ctrl+W,W: 浏览器窗口  Ctrl+W,S: 解决方案管理器  Ctrl+W,C: 类视图  Ctrl+W,E: 错误列表  Ctrl+W,O: 输出视图  trl+W,P: ...

  8. mysql性能监控工具Innotop

    mysql监控管理工具--innotop 1.innotop安装参考官网:http://innotop.googlecode.com/svn/html/installing.html # wget h ...

  9. Assets理解随笔

    在PlayFramework中应用 在Play框架中提供的都是动态文件响应,前端工作内容大部分是静态文件.Assets大概起的就是这个作用. 默认路径看 conf/routes 里: # Map st ...

  10. 【grunt整合版】学会使用grunt打包前端代码

    grunt 是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:① 压缩文件② 合并文件③ 简单语法检查 对于其他用法,我还不太清楚,我们这里简单介绍下grunt的压缩.合并文件,初学, ...