HDU-4312-meeting point-2-mathematics + ideas

Source: Internet
Author: User
The distance from a square to eight neighboring points is 1. http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4312

Give you the coordinates of multiple points, so that you can find a point to meet the shortest distance from other points.

Solution:

In fact, it is also a brute-force enumeration. It seems that there is no better way than him. But do some preprocessing during enumeration. For more information, see 4311.

Others need to know about the transformation between the Chebyshev distance and Manhattan distance.

I listened to the question and finally thought it was correct.

The Chebyshev distance between two points on the plane is max (| x1-x2 |, | y1-y2 |)

 

Chebyshev Manhattan

For the Chebyshev distance between two points in the original coordinate system, it is to rotate the coordinate axis 45 degrees clockwise and enlarge the coordinate values of all points in SQRT (2) 1/2 of the Manhattan distance in the new coordinate system.

You can draw a picture ......

Assume that there are two points (x1, Y1), (X2, Y2), you may set x1> X2 (otherwise, you can exchange these two points ).

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

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

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

It can be discussed in four situations:

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)

Therefore, the Chebyshev distance form is converted into the Manhattan distance form before solving the problem. For the solution process, see meeting point-2.


I changed the code 4311 to three lines...

# Include <iostream> # include <algorithm> # include <cstring> # include <cstdio> # define maxn 100010 typedef long l; using namespace STD; struct node {long X, y; int ID;} p [maxn]; long sumx [maxn], Sumy [maxn]; bool cmpx (node A, Node B) {return. x <B. x;} bool cmpy (node A, Node B) {return. Y <B. y;} int t, n; long tmpx, tmpy; // int main () {scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) {scanf ("% i64d % i64d ", & tmpx, & tmpy); P [I]. X = tmpx-tmpy; // change the position P [I]. y = tmpx + tmpy; // changed location} Sort (p + 1, p + 1 + N, cmpx); sumx [1] = P [1]. x; P [1]. id = 1; for (INT I = 2; I <= N; ++ I) {sumx [I] = sumx [I-1] + P [I]. x; P [I]. id = I;} Sort (p + 1, p + 1 + N, cmpy); Sumy [1] = P [1]. y; For (INT I = 2; I <= N; ++ I) {Sumy [I] = Sumy [I-1] + P [I]. y;} long ans = (1ll <60); // For (INT I = 1; I <= N; I ++) {Int J = P [I]. ID; long YY = (P [I]. y) x (I * 1.0)-Sumy [I]; YY + = (Sumy [N]-Sumy [I]-(P [I]. y) * (n-I) x 1.0); long xx = (P [I]. x) * (J * 1.0)-sumx [J]; xx + = (sumx [N]-sumx [J]-(P [I]. x) * (n-j) * 1.0); xx + = YY; ans = min (XX, ANS);} printf ("% i64d \ n ", ANS/2);} return 0 ;}

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.