! HDU 4311 min Manhattan distance-thinking & card time-(horizontal ordinate separate calculation, sort)

Source: Internet
Author: User
Tags sorts

Test instructions: There are n points, which is the starting point for a point in this N-point, and the distance and minimum of Manhattan to the points

Analysis:

Violent enumeration and time-outs, the problem is generally test thinking, mostly with the skills to find an efficient method. Personally think this question is a type of topic with the previous article. This thought should be remembered.

This question is also used "divide the rule", although the question asks is the Manhattan distance, but why we really need to one step, can cross ordinate separately to seek , first x sorts, then iterates over, seeks out the horizontal axis distance, then the Y sorts, The distance between the coordinates and the distance from the x just obtained is the distance from Manhattan.

Here is a very ingenious but actually obvious thing: assuming now that we have x ranked by the ABC three points respectively, then c to ab distance and is | c-a|+| C-b|, and because already sorted, then the absolute value can be removed, get (c-a) + (c-b), then is 2*c-(A+B), that is, a point to its front of the point of the distance and equal to its front of the number of points multiplied by its own again minus the front of a bit and, Do you think it's time to ask for a sequence of numbers and we go through the sequence of numbers, and by this method we get a point to the point in front of it with O (n) and then go backwards through the sequence with similar thoughts to the distance to the point behind it, plus the line.

There is also a separate x, y sort sum, but you can do this: first directly to find the front point to all points of the distance, and then sequentially traverse the sequence, through the back of a point with the previous point of the relationship, to find the difference between the subtraction, that is. If you don't understand, think carefully or draw a picture.

Code:

#include <iostream> #include <cstring> #include <algorithm> #define INF 1000000000000007using Namespace Std;struct node{long long X,y;long long sum;} A[100005];bool CMP1 (node A,node b) {return a.x<b.x;} BOOL CMP2 (node A,node b) {return a.y<b.y;} int main () {int T,n;cin>>t;while (t--) {Cin>>n;long long ans=inf;memset (a,0,sizeof (0)); for (int i=0;i<n; i++)  Cin>>a[i].x>>a[i].y;sort (A,A+N,CMP1); long long sum=0;for (int i=0;i<n;i++) {A[i].sum=i*a[i] . x-sum;sum+=a[i].x;} sum=0;for (int i=n-1;i>=0;i--) {a[i].sum+=sum-(n-1-i) *a[i].x;sum+=a[i].x;} Sort (A,A+N,CMP2); sum=0;for (int i=0;i<n;i++) {a[i].sum+=i*a[i].y-sum;sum+=a[i].y;} sum=0;for (int i=n-1;i>=0;i--) {a[i].sum+=sum-(n-1-i) *a[i].y;sum+=a[i].y;ans=min (ans,a[i].sum);} Cout<<ans<<endl;}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU 4311 min Manhattan distance-thinking & card time-(horizontal ordinate separately, sort)

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.