标签:
题目的距离为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 )
标签:
原文地址:http://www.cnblogs.com/JSZX11556/p/5111006.html