HDU 4311&4312 Meeting Point-1&2 (Manhattan distance && Chebyshev distance)

Source: Internet
Author: User
Tags bitset cmath

HDU 4311

Test instructions: There are n points on the plane, one point (x, y) can only reach (X-1,y), (X+1,y), (×, y-1), (x, y+1) 4 points. Find a point from N points to minimize the sum of the distance from the other point to this point.

Ideas:

It can be found that the distance between the two points is |x1-x2| + |y1-y2|, which is the distance between two points of Manhattan.

The naïve approach is to traverse all points and enumerate the sum of the distance between the point and the other points, but it will be tle;

Trickery's approach is to sort all points with the center point of the Manhattan distance, enumerating about 250 points in the middle of the situation comparison can be (do not bully people data water!).

Correct posture:

Keep input data with struct array p[i] x[i],y[i]

The x values of the points are sorted in ascending order to find the first I items x and Sum1[i], and the Y values of the points are sorted in ascending order to find the first I items y and sum2[i].

Find the position of the p[i].x in the sorted x[] ox,p[i].y the position in the sorted y[] Oy, and the sum of the Manhattan distances of the selected center point as p[i]

(x_[ox]* (ox-1)-sum1[ox-1]) + (sum1[n]-sum1[ox]-x_[ox]* (N-ox)) + (y_[oy]* (oy-1)-sum2[oy-1]) + (Sum2[n]-sum2[oy]-y_[oy ]* (N-oy));
This formula means that there are ox-1 points of x value is less than p[i],n-ox more than p[i], it can eliminate the absolute value of the symbol, the first half of the distance of Manhattan to find out. The second part is the same.

Code

/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;const int MAXN = 100000 + 5;int N;lint x_[maxn],y_[m Axn];lint sum1[maxn],sum2[maxn];struct pt{int x, y;}; PT p[maxn];void Input () {cin >> n;for (int i = 1; I <= n; i++) {scanf ("%i64d%i64d", &x_[i], &y_[i]);p [i].x = X_[i];p [I].Y = Y_[i];} Sort (x_+1,x_+n+1), CLS (SUM1), for (int i = 1; I <= n; i++) sum1[i] = Sum1[i-1] + x_[i];sort (y_+1,y_+n+1); CLS (SUM2);(int i = 1; I <= n; i++) sum2[i] = Sum2[i-1] + y_[i];} void Solve () {Lint ans = 1e18;for (int i = 1; I <= n; i++) {Lint ox = Lower_bound (x_+1, x_+n+1, p[i].x)-(x_); Lint O        y = Lower_bound (y_+1, y_+n+1, P[i].y)-(Y_);        Lint tmp = 0;        TMP + = (x_[ox]* (ox-1)-sum1[ox-1]) + (sum1[n]-sum1[ox]-x_[ox]* (N-ox)); TMP + = (y_[oy]* (oy-1)-sum2[oy-1]) + (sum2[n]-sum2[oy]-y_[oy]* (N-oy)); ans = min (ans, tmp);} cout << ans << endl;} int main () {int t;cin >> t;while (t--) {input (); Solve ();} return 0;}


HDU 4312

Test instructions: There are n points on the plane, one point (x, y) can only reach (X-1,y), (X+1,y), (×, y-1), (x, Y+1), (x-1,y+1), (x-1,y-1), (x+1,y+1), (x+1,y-1) 8 points. Find a point from N points to minimize the sum of the distance from the other point to this point.

Idea: It can be found that the distance between the two points is Max (|x1-x2|, |y1-y2|), which is the Chebyshev distance.

We can rotate the axis 45 degrees clockwise and enlarge the coordinate values of all the points sqrt (2) times,

Chebyshev is One-second of the distance from Manhattan in the new coordinate system.

Then follow the above-mentioned practice directly to be able to.

The proof is as follows://Proof fromhttp://blog.csdn.net/dgq8211/article/details/7796711

Suppose there are two points (x1,y1), (X2,y2), may wish to set x1>x2.

Then Chebyshev distance D1 = max (|x1-x2|, |y1-y2|)

These two points correspond to the coordinates in the new coordinate system (X1-Y1, X1+y1), (X2-y2, X2+y2)

The coordinates of the point (x, Y) change to (Cosαx-sinαy, Sinαx + cosαy) After a point rotates counterclockwise α° (or axis clockwise) around the origin.

Then Manhattan distance D2 = |x1-y1-x2+y2| + |x1+y1-x2-y2|

There are four kinds of situations to discuss:

1.1 Y1>y2 && X1-x2>y1-y2

D1 = Max (x1-x2, y1-y2) = x1-x2

D2 = x1-y1-x2+y2 + x1+y1-x2-y2 = 2 (x1-x2)

1.2 Y1>y2 && X1-x2<=y1-y2

D1 = max (x1-x2,y1-y2) = Y1-y2

D2 =-(x1-y1-x2+y2) + X1+y1-x2-y2 = 2 (y1-y2)

2.1 Y1<=y2 && X1-x2>y2-y1

D1 = Max (x1-x2, y2-y1) = x1-x2

D2 = x1-y1-x2+y2 + x1+y1-x2-y2 = 2 (x1-x2)

2.2 Y1<=y2 && X1-x2<=y2-y1

D1 = Max (x1-x2, y2-y1) = Y2-y1

D2 = X1-y1-x2+y2-(x1+y1-x2-y2) = 2 (y2-y1)

Then, the form of Chebyshev distance into Manhattan distance can be solved.


Code

/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;const int MAXN = 100000 + 5;int N;int X[MAXN],Y[MAXN ];lint sumx[maxn],sumy[maxn];lint s1,s2;struct pt{int x, y;} P[maxn];void input () {cin >> n;for (int i = 0; i < n; i++) {scanf ("%d%d", &x[i],&y[i]); int tmp = X[i];x[i] + = Y[i];y[i] = tmp-y[i];p [i].x = X[i];p [I].Y = Y[i];}} void Solve () {sort (x,x+n), sort (y,y+n), CLS (SUMX), CLS (Sumy), for (int i = 1; I <=n; i++) {Sumx[i] = Sumx[i-1] + x[i-1];sumy[i] = Sumy[i-1] + y[i-1];} Lint ans = 1e18;for (int i = 0; i < n; i++) {Lint px = lower_bound (x, X+n, p[i].x)-X;lint py = Lower_bound (y, Y+n , P[I].Y)-y;lint tmp = 0;lint tmpx = (2*px-n) *p[i].x + sumx[n]-2*sumx[px];lint tmpy = (2*py-n) *p[i].y + sumy[n]- 2*sumy[py];tmp = tmpx + tmpy;ans = min (tmp, ans);} cout << ans/2 << Endl;} int main () {//freopen ("Input.txt", "R", stdin), int t;cin >> t;while (t--) {input (); solve (); return 0;}


PS. There is also a distance called Euler distance, which is d = sqrt ((x1-x2) ^2 + (y1-y2) ^2)

Copyright notice: Bo Master said authorized all reproduced:)

HDU 4311&4312 Meeting Point-1&2 (Manhattan distance && Chebyshev distance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.