01_ Legendary car (fabled Rooks UVa 11134 greedy problem)

Source: Internet
Author: User

problem Source: Rujia "algorithmic Competition Primer-Training Guide" P81:

problem Description: your task is to put N vehicles on the N*n (1<=n<=5000) board, so that any two vehicles do not attack each other, and the first I vehicle within a given rectangle R.

Problem Analysis: 1. The most critical point is that each car's x-coordinate and y-coordinate can be considered separately (they do not affect each other), or it will become very complex, the title becomes two interval point of choice problem: so that each car in a given range to choose a spot, any two cars can not choose the same point.

2. Another key point is the greedy method of choice, greedy method: The range of all points, according to the right end point from small to large, each time in an interval at the point of selection, from left to right is selected by the previous interval selected points. (from this interval to select the maximum extent to prevent the subsequent interval is not selectable (because the right end point selected is the smallest))

       the wrong greedy method: sort all the intervals by the left, and then select the leftmost one at a time. counter example: [1,1],[1,3],[2,2];(This greedy hair does not guarantee that the future range is a bit optional, some of the interval may be longer, take the latter point more appropriate)

Example Links:Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem &problem=2075

Example: UVa 11134

11134-fabled Rooks

Time limit:3.000 seconds

We would like-to-place n rooks, 1≤ n ≤5000, on a nxn board subject to the following RESTRICTI Ons

    • the  i -th rook can only is placed within the rectangle given by its left-upper corner ( xli ,   Yli ) and its right-lower corner ( xri ,   Yri ), where 1 ≤  i  ≤  n , 1 ≤  xli  ≤  xri  ≤  n , 1 ≤   yli  ≤  yri  ≤  n .
    • No. Rooks can attack each of the other, which is No and rooks can occupy the same column or the same row.

The input consists of several test cases. The first line of each of them contains one integer number, n, the side of the board. n lines follow giving the rectangles where the rooks can be placed as described above. The i-th line among them gives XLI, yli, xri, and Yri. The input file is terminated with the integer '0' in a line by itself.

Your task is to find such a placing of rooks, the above conditions is satisfied and then output n lines each Giving the position of a rook in order in which their rectangles appeared in the input. If There is multiple solutions, any one would do. Output Impossible If there is no such placing of the rooks.

Sample input
81 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 881 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80
Output for sample input
1 15 82 44 27 38 56 63 71 15 82 44 27 38 56 63 7

Code implementation:

1#include"stdio.h"2#include"string.h"3#include"algorithm"4 using namespacestd;5 6 #defineN 50107 8typedefstruct9 {Ten     intID; One     intL,r; A }point; -  - intN; the BOOLMark[n]; - Point x[n],y[n],ans[n]; - Point ansx[n],ansy[n]; -  + BOOLCMP (point A,point b) {returnA.R < B.R; }//Sort by the smallest right endpoint -  + BOOLCMP1 (Point A,point b) {returna.ID < b.id;}//restore order by ID number A  at BOOLSolve (Point *a,point *ans) - { -     inti,j; -memset (Mark,false,sizeof(Mark)); -      for(i=0; i<n; i++) -     { in          for(J=A[I].L; j<=a[i].r; J + +) -         { to             if(Mark[j])Continue; +              Break; -         } the         if(J&GT;A[I].R)return false; *ANS[I].L = j;//Save the answer with ANS[I].L $Ans[i].id =a[i].id;Panax NotoginsengMARK[J] =true; -     } the     return true; + } A  the intMain () + { -     inti; $      while(~SCANF ("%d", &n), n!=0) $     { -          for(i=0; i<n; i++) -         { thescanf"%d %d%d%d",&x[i].l,&y[i].l,&x[i].r,&Y[I].R); -X[i].id = Y[i].id =i;Wuyi         } theSort (x,x+n,cmp); -Sort (y,y+n,cmp); Wu         if(Solve (X,ANSX) &&solve (Y,ansy)) -         { AboutSort (ansx,ansx+n,cmp1); $Sort (ansy,ansy+n,cmp1); -              for(i=0; i<n; i++) -printf"%d%d\n", ANSX[I].L,ANSY[I].L); -         } A         Else +printf"impossible\n"); the     } -     return 0; $}

01_ Legendary car (fabled Rooks UVa 11134 greedy problem)

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.