HDU 4311 meeting point-1 & HDU 4312 meeting point-2

Source: Internet
Author: User

Question: give n (<1e5) points to find a point as the place of the Party, so that each point can reach here at least. 4311 is the distance from Manhattan, 4312 is the distance from cherbihov;

Manhattan distance: We all know that the Manhattan distance between a (XA, Yb) and B (XB, Yb) in the two-dimensional coordinate system is ABS (xA-xB) + ABS (ya-Yb );

We can see that this distance is related to X and Y, but x and y do not affect each other. Therefore, we can calculate them separately. The advantages of separate calculation are as follows:

If we only provide a coordinate system for one dimension, we cannot process it in a complex time? It is difficult to sort the data first, and we will find that we need O (n) Time to calculate the answer we want.

The merge operation of the two dimensions only requires O (n) records;

Distance from cherbihov:

We know that a square such:

It is also known that a square can be formed for the point whose distance from a vertex is R, for example:

   

Comparing the two pictures, we know that the distance between the two images can be converted to the distance between the two dots into the distance between the Manhattan (because we have already discussed how to calculate the distance between the Manhattan )!!!;

Troublesome? : But it is actually very simple: we know that the square rotation of cherbieve above is a certain multiple of the transformation, and it will overlap with the square in Manhattan! So we can think that the distance between cherbihov is the result of the enlargement of the distance from Manhattan: In turn, we only need to make the same rotation and amplification of the coordinates;

The Code is as follows :::

# Include <cstdio> # include <map> # include <vector> # include <set> # include <iostream> using namespace STD; typedef long ll; struct info {ll X, y; int CNT; Info () {}info (int x, int y): x (x), y (y) {}}; bool cmpx (info, info B) {if (. X = B. x) return. Y <B. y; return. x <B. x;} bool cmpy (info a, info B) {if (. y = B. y) return. x <B. x; return. Y <B. y;} info ko1 [100005]; Info ko2 [100005]; ll dp1 [100005]; ll dp2 [100005]; int N; void make (int x) {int tmpx, tmpy; tmpx = ko1 [X]. x; tmpy = ko1 [X]. y; // The value is counter-clockwise based on the distance. // You can also rotate the value by another point. The rotation point must be a // column. // tmpx = ko1 [x]. x-2; // tmpy = ko1 [X]. Y + 12; ko1 [X]. X = tmpx-tmpy; ko1 [X]. y = tmpy + tmpx;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); For (INT I = 1; I <= N; I ++) {// CIN> ko1 [I]. x> ko1 [I]. y; scanf ("% i64d % i64d", & ko1 [I]. x, & ko1 [I]. y); Make (I); // if this sentence is 12, this sentence is 11} Sort (ko1 + 1, ko1 + 1 + N, cmpx ); for (INT I = 1; I <= N; I ++) {ko1 [I]. CNT = I; ko2 [I] = ko1 [I];} Sort (ko2 + 1, ko2 + 1 + N, cmpy); For (INT I = 1; I <= N; I ++) {dp1 [I] = dp1 [I-1] + ABS (ko1 [I]. x-ko1 [1]. x); dp2 [I] = dp2 [I-1] + ABS (ko2 [I]. y-ko2 [1]. y);} ll ans = 1ll <62; For (INT I = 1; I <= N; I ++) {ll tmp2 = dp2 [N]-dp2 [I]-(ko2 [I]. y-ko2 [1]. y) * (N-I) + (ko2 [I]. y-ko2 [1]. y) * i-dp2 [I]; int CNT = ko2 [I]. CNT; ll tmp1 = dp1 [N]-dp1 [CNT]-(ko1 [CNT]. x-ko1 [1]. x) * (N-CNT) + (ko1 [CNT]. x-ko1 [1]. x) * cnt-dp1 [CNT]; ans = min (ANS, (tmp1 + tmp2)/2); // This 12 is divided by 2; 11 not required} cout <ans <Endl;} 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.