"Bzoj" "3170" "TJOI2103" Squirrel Party

Source: Internet
Author: User

Chebyshev distance + Manhattan Distance

Puzzle: http://www.cnblogs.com/zyfzyf/p/4105456.html

In fact, we should do this first and then do "Bzoj" "3210" flower God watering the flower Assembly bar ...

We found that Dist (I,J) is the Chebyshev distance between I and J, so again, we can turn them into Manhattan to do. (X[i]=x+y; y[i]=x-y;)

Then it is the minimum value of the Manhattan distance from the N point to a certain point.

Because it is Manhattan distance, so x and Y are irrelevant, we can calculate separately!

For the x sort, we can find that the distance from all points to x[1] is $\sum_{i=2}^n (x[i]-x[1]) =\sum_{i=2}^n X[i]-x[1]* (n-1) $

We find which of the $\sum_{i=2}^n x[i]$ is actually the suffix and.

Similarly, to x[2] is also the prefix and & suffix and related, then you can O (n) to sweep over to do ...

Y-direction ... And sweep it again, and sweep it backwards.

1 /**************************************************************2 problem:31703 User:tunix4 language:c++5 result:accepted6 time:424 Ms7 memory:2840 KB8 ****************************************************************/9  Ten //Bzoj 3170 One#include <cstdio> A#include <cstring> -#include <cstdlib> -#include <iostream> the#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + #definePB Push_back - using namespacestd; +typedefLong LongLL; AInlineintGetint () { at     intR=1, v=0;CharCh=GetChar (); -      for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') r=-1; -      for(; isdigit (ch); Ch=getchar ()) v=v*Ten-'0'+ch; -     returnr*v; - } - Const intn=1e5+Ten; in /*******************template********************/ -   to intN; + structnode{ -     intx, y; LL Z; the }a[n]; *InlineBOOLCMP1 (node A,node b) {returna.x<b.x;} $InlineBOOLCMP2 (node A,node b) {returna.y<b.y;}Panax Notoginseng intMain () { - #ifndef Online_judge theFreopen ("3170.in","R", stdin); +Freopen ("3170.out","W", stdout); A #endif then=getint (); +F (I,1, N) { -         intX=getint (), y=getint (); $A[i].x=x+y; a[i].y=x-y; $     } -Sort (A +1, a+n+1, CMP1); -LL sum=0; theF (I,1, N) { -a[i].z+= (LL) a[i].x* (i-1)-sum;Wuyisum+=a[i].x; the     } -sum=0; WuD (I,n,1){ -a[i].z+=sum-(LL) a[i].x* (ni); Aboutsum+=a[i].x; $     } -Sort (A +1, a+n+1, CMP2); -sum=0; -F (I,1, N) { Aa[i].z+= (LL) a[i].y* (i-1)-sum; +sum+=a[i].y; the     } -sum=0; $D (I,n,1){ thea[i].z+=sum-(LL) a[i].y* (ni); thesum+=a[i].y; the     } theLL ans=1e18; -F (I,1, N) ans=min (ans,a[i].z); inprintf"%lld\n",ans>>1); the     return 0; the}
View Code 3170: [Tjoi 2013] Squirrel party Time limit:10 Sec Memory limit:128 MB
submit:720 solved:326
[Submit] [Status] [Discuss] Description

There are n little squirrels, and their home a dot x, y indicates that the distance between the two points is defined as: Point (x, y) and the 8 points around it that is four points and diagonally four points above and below, and a distance of 1. Now n a squirrel to go to a squirrel home, beg to pass the shortest distance.

Input

The first line gives the number n, which indicates how many little squirrels there are. 0<=n<=10^5
The next n lines, each row gives the coordinates of X, Y for their home.
-10^9<=x,y<=10^9

Output

Indicate the distance and the minimum for the party to go.

Sample Input6
-4-1
-1-2
2-4
0 2
0 3
5-2Sample Output -Hintsource [Submit] [Status] [Discuss]

"Bzoj" "3170" "TJOI2103" Squirrel Party

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.