Distance of the topic for Max (|x1-x2|, |y1-y2|) (Chebyshev distance). Chebyshev distance (x, y)-Manhattan Distance ((x+y)/2, (x-y)/2) (Manhattan (x, y)-Chebyshev (X+y, XY)). After turning into Manhattan distance, sort prefixes and maintenance.
--------------------------------------------------------------------------
#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 <<;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] Squirrel party Time limit: ten Sec Memory Limit: MB
Submit: 874 Solved: 421
[Submit] [Status] [Discuss] Description
There are n little squirrels, and their home a dot x, y indicates that the distance between the two points is defined as: Point (x, y) and the 8 points around it that is four points and diagonally four points above and below, and a distance of 1. Now n a squirrel to go to a squirrel home, beg to pass the shortest distance.
Input
The first line gives the number n, which indicates how many little squirrels there are. 0<=n<=10^5
The next n lines, each row gives the coordinates of X, Y for their home.
-10^9<=x,y<=10^9
Output
Indicate the distance and the minimum for the party to go.
Sample Input6
-4-1
-1-2
2-4
0 2
0 3
5-2Sample Output -HINT
Source
Bzoj 3170: [Tjoi 2013] Squirrel Party (sort)