Test instructions: n points on the surface, a point to the other points of the Manhattan distance minimum and, Chebyshev distance minimum and.
Idea: for Chebyshev distance can be converted to Hamiltonian distance, the method is to rotate the coordinates of each point counterclockwise 45 degrees and then enlarge sqrt (2) times, replaced by coordinate means (x, y) and (x-y,x+y).
For the first question, the Manhattan distance is the smallest and the sum (xj-xi) +sum (yj-yi).
If the immediate complexity of the time can not withstand.
So we can sort x first, for left-to-right sum (x) we can hand out, i.e. sumx[tj[i].id] = Sumx[tj[i-1].id] + (2*i-n) * (tj[i].x-tj[i-1].x);
For Y.
4312 Code included
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL long long #define PII pair<i Nt,int>using namespace Std; const int MAXN = 100000 + 500;//const int INF = 0x3f3f3f3f; LL DISX[MAXN], DISY[MAXN]; LL sumv[maxn];struct Point {int x, y, id; Point (int x=0, int y=0, int. z=0): X (x), Y (y), id (z) {}} tj[maxn];bool Cmp1 (Point A, point B) {return a.x < b.x;} BOOL Cmp2 (Point A, point B) {return a.y < b.y;} int main () {//freopen ("Input.txt", "R", stdin); int t; Cin >> T;while (t--) {int n;cin >> n;for (int i = 0; I < ; N i++) {int u, v;scanf ("%d%d", &u, &v); Tj[i] = Point (U-v, u+v, i);} Sort (TJ, Tj+n, CMP1);d isx[tj[0].id] = 0;for (int i = 1; i < n; i++) disx[tj[0].id] + = (ll) tj[i].x-(LL) tj[0].x;for (int i = 1; i < n; i++) disx[tj[i].id] = Disx[tj[i-1].id] + (LL) (2*i-n) * (LL) (tj[i].x-tj[i-1].x); sort (TJ, Tj+n, CMP2) ;d Isy[tj[0].id] = 0;for (int i = 1; i < n; i++) disy[tj[0].id] + = (ll) Tj[i].y-(LL) tj[0].y;for (int i = 1; i < n; i++ ) disy[tj[i].id] = Disy[tj[i-1].id] + (LL) (2*i-n) * (LL) (TJ[I].Y-TJ[I-1].Y); LL ans = 100000000000000000;for (int i = 0; i < n; i++) {sumv[i] = Disx[i] + disy[i];//cout << disx[i] << " "<< disy[i] << endl;ans = min (ans, sumv[i]);} cout << ans/2 << Endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4311,4312 Meeting Point (Manhattan distance, Chebyshev distance)