The movement of the Tower/point (Luo gu 1632)

Source: Internet
Author: User

"Problem description"

There are n integer coordinate points on a plane. If you move the point (x0,y0) to (X1,Y1), the cost is |x0-x1|+|y0-y1|. The minimum cost of K (K=1,..., N) points in the same position.

"Sample Input"

4
15 14
15 16
14 15
16 15

"Sample Output"

0
2
3
4

"Problem-solving ideas"

At first look at this problem, also thought is can only move to a bit of place, that is to move the other point to a point, moving (k-1) points need the minimum cost, the results found that the sample are not able to pass ... Then analyzed the sample, found that the sample is to move the K-point to the (15,15) up, of course, k=1 do not need to move, so I went to find all the median of x, Y, the results of the sample, but the OJ on the 0 points, then we re-analyze the topic.

The topic can be moved to any point, we are not difficult to find that the point of X must be in all x-coordinates, the same, Y is also so, so we just enumerate each x, Y and then each point will be moved once, find the move K points, the minimum movement cost can be.

So we can use a triple loop in this problem, because the value of n is not large, only 50, and the triple loop does not have a tle. The function of the triple loop here is to conveniently calculate the cost of each point movement, as described in the code.

"Code Implementation"
1 typeRec=Record  2 X,y:longint; 3 End; 4 varA:Array[0.. -] ofRec; 5F:Array[0.. -,0.. -] ofLongint; 6D,ans:Array[0.. -] ofLongint; 7 I,j,n,k,s:longint; 8 proceduresort (l,r:longint); 9 varI,j,x,y:longint; Ten begin   Onei:=l;  Aj:=R;  -x:=d[(l+r) shr1];  -  Repeat   the    whileD[i]<x Do   - Inc (I);  -    whileX&LT;D[J] Do   - Dec (j);  +   if  not(I&GT;J) Then   -    begin   +y:=D[i];  Ad[i]:=D[j];  atd[j]:=y;  - Inc (I);  -j:=j-1;  -    End;  -  untilI>J;  -  ifL<j Then   in sort (l,j);  -  ifI<r Then   to sort (i,r);  + End;  - begin   the READLN (n);  *   fori:=1  toN Do   $    withA[i] Do  Panax Notoginseng readln (x, y);  -   fori:=1  toN Do   theans[i]:=Maxlongint;  +   fori:=1  toN Do   A    forj:=1  toN Do   the    begin   +      fork:=1  toN Do   -D[k]:=abs (a[k].x-a[i].x) +abs (A[K].Y-A[J].Y); //enumerates the costs required to move each point to the Xi,yj.  $Sort1, N);  $s:=0;  -      fork:=1  toN Do   -      begin   thes:=s+D[k];  -       ifS&LT;ANS[K] Then  Wuyians[k]:=R;  the      End; //Update the optimal solution -    End;  Wu   fori:=1  toN Do   - Writeln (Ans[i]);  About End.

The movement of the Tower/point (Luo gu 1632)

Related Article

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.