! HDU 4311 minimum Manhattan distance-Thinking & amp; card time-(separated and sorted by horizontal and vertical coordinates), hdu Manhattan

Source: Internet
Author: User

! HDU 4311 minimum Manhattan distance-Thinking & timing-(separated and sorted by horizontal and vertical coordinates), hdu Manhattan

Question: There are n points. Start from a point in the n points and find the distance and minimum value of the Manhattan point.

Analysis:

Brute-force enumeration times out again. This type of question is generally considered, and most of them use skills to find an efficient method. I personally think this question is similar to the question in the previous article. Remember this idea.

This is also the use of "Divide Governance". Although the question requires the distance from Manhattan, why do we really need to find it in one step? We can separate it by vertical and horizontal coordinates, and sort it by x first, next, traverse it and find the distance of the horizontal coordinate, then sort by y, traverse it and find the distance of the coordinate, and add it to the distance of the obtained x, that is, the distance of the Manhattan.

Here is a very clever but obvious thing: assuming that we have sorted by x, there are three points in ABC, respectively, so C to AB distance and is | C-A | + | C-B |, And because has been sorted, then the absolute value can be removed, (C-A) + (C-B ), 2 * C-(A + B ), that is to say, the sum of the distance between a vertex and its front point is equal to the number of vertices before it multiplied by its own and then the sum of all vertices before it, so far, do you want to obtain the sum of a series by traversing the series? In this way, we use O (n) we get the distance between a point and the point before it, and then traverse the series and add the distance from it to the point after it with a similar idea.

Another method is to separate x and y for sorting and summation. However, we can do this: first, we can directly find the distance from the first vertex to all vertices, and then traverse the series in sequence, find the difference value minus by the relationship between the next vertex and the previous vertex. If you don't understand it, think about it 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.