Converting (x, y) to (x+y,x-y) converts the Chebyshev distance into a Manhattan distance (push it yourself) A, B's Chebyshev distance is half the distance from a ' and B ' Manhattan. Then you can separate x, Y, and sort the median. Note that if the last selected optimal x and Y return is not an integer, choose an optimal scheme around the top and bottom of it.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std; typedef long Long LL; #define N 100010 int n;int x, y; int x[n],y[n]; ll check (int x,int y) { ll res (0); for (int i=1;i<=n;i++) res+=abs (X-x[i]) +abs (Y-y[i]); return res;} int main () { scanf ("%d", &n); for (int i=1;i<=n;i++) { scanf ("%d%d", &x,&y); X[i]=x+y; y[i]=x-y; } Sort (x+1,x+n+1); Sort (y+1,y+n+1); x=x[n+1>>1]; y=y[n+1>>1]; if (~ (x ^ y) & 1) printf ("%lld\n", check (x, y) >>1); else printf ("%lld\n", Min (min (check (x+1,y), check (x-1,y)), Min (check (x,y+1), check (x,y-1))), >>1); return 0;}
"bzoj3210" flower-watering gathering of the gods