The Skyline problem

Source: Internet
Author: User

A city's skyline is the outer contour of the silhouette formed by all the buildings in then city then viewed from a Distan Ce. Now suppose you is given the locations and height of all the buildings as shown in a cityscape photo (figure A), write a Program to output the skyline formed by these buildings collectively (Figure B).

The geometric information of each building was represented by a triplet of integers [Li, Ri, Hi] , where and is the Li Ri x Coordinates of the left and right edge of the ith building, respectively, and are its Hi height. It's guaranteed that 0 ≤ Li, Ri ≤ INT_MAX , 0 < Hi ≤ INT_MAX and Ri - Li > 0 . Assume all buildings is perfect rectangles grounded on a absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A is recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ]  .

The output is a list of "key points" (Red dots in Figure B) in the format of that [ [x1,y1], [x2, y2], [x3, y3], ... ] uniquely defines a skyline. A key point was the left endpoint of a horizontal line segment. Note the last key point, where the rightmost building ends, was merely used to mark the termination of the skyline, an D always have zero height. Also, the ground in between any and adjacent buildings should is considered part of the Skyline Contour.

For instance, the skyline in Figure B should is represented as: [ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ] .

Notes:

    • The number of buildings in all input list is guaranteed to being in the range [0, 10000] .
    • The input list is already sorted in ascending order by the left X position Li .
    • The output list must is sorted by the X position.
    • There must is no consecutive horizontal lines of equal height in the output skyline. For instance, was not [...[2 3], [4 5], [7 5], [11 5], [12 7]...] acceptable; The three lines of height 5 should being merged into one in the final output as such:[...[2 3], [4 5], [12 7], ...]

Credits:
Special thanks to @stellari for adding this problem, creating these both awesome images and all test cases.

Use the scan line method for processing. Click on the left to mark the entry, the right point marked for departure, real-time maintenance "activity building list." Place the entry point of the same horizontal line in front, leaving the point behind. First, the highest value of the entry point at the X point is judged, and the points are added to the activity floor list, then the highest value of the exit point is determined, and the points are removed from the Activity building list, and the maximum height of the current Activity building list is output if the highest value equals the current height.

PS: When you use Set or multiset to maintain a list of active buildings, when you delete a departure point height, all the same values for that height are deleted, resulting in an "active building list" height of 0, resulting in an error, so you need to maintain a list of heights when entering data, and in the "Activity building list" The height of the entry point is recorded in the coordinates of the height list, avoiding the simultaneous deletion of points of the same height.

1 classSolution {2 Private:3     #defineLeft 0;4     #defineRight 1;5     structXEVENT6     {7         intx;8         intHeight_index;9         intside;TenXEVENT (int_x,int_height,int_side): X (_x), Height_index (_height), side (_side) {} One     }; A Private: - Static    BOOLCompareevent (Constxevent& E1,Constxevent&E2) -     { the         if(e1.x!=e2.x) -             returne1.x<e2.x; -         returne1.side<e2.side; -     } +  Public: -vector<pair<int,int>> Getskyline (vector<vector<int>>&buildings) { +         intn=buildings.size (); A          atvector<pair<int,int>>Res; -         if(n<1)  -             returnRes; -Vector<xevent>Event; -vector<int>Buildingheight; -         Set<int>activebuilding; inActivebuilding.insert (0); -          to          for(intI=0; I<buildings.size (); i++) +         { -Auto &b=Buildings[i]; the             intindex=buildingheight.size (); *             Event. Push_back (XEVENT (b[0],index,0)); $            Panax Notoginseng             Event. Push_back (XEVENT (b[1],index,1)); -Buildingheight.push_back (b[2]); the         } +SortEvent. Begin (),Event. End (), compareevent); A         intcurheight=0; thepair<int,int>Tmp_pair; +          for(intI=0;i<Event. Size (); i++) -         { $             if(Event[i].side==0) $             { -Activebuilding.insert (Event[I].height_index]; -                 intnewheight=buildingheight[Event[I].height_index]; the                 intnewx=Event[i].x; -                  while(i+1<Event. Size () &&Event[i+1].x==newx&&Event[i+1].side==0)Wuyi                 { thei++; -Activebuilding.insert (Event[I].height_index]; WuNewheight=max (newheight,buildingheight[Event[I].height_index]); -                 } About                 if(newheight>curheight) $                 { -Res.push_back (tmp_pair=Make_pair (newx,newheight)); -curheight=Newheight; -                 } A             } +             Else the             { -Activebuilding.erase (Event[I].height_index]; $                 intnewheight=buildingheight[Event[I].height_index]; the                 intnewx=Event[i].x; the                  while(i+1<Event. Size () &&Event[i+1].x==Event[i].x&&Event[i+1].side==1) the                 { thei++; -Activebuilding.erase (Event[I].height_index]; inNewheight=max (newheight,buildingheight[Event[I].height_index]); the                 } the                 if(newheight==curheight) About                 { the                     intmaxheight=0; themultiset<int:: Iterator it=Activebuilding.begin (); the                      for(; It!=activebuilding.end (); it++) +                     { -Maxheight=max (maxheight,buildingheight[*it]); the                     }Bayi                     if(maxheight<newheight) the                     { theRes.push_back (tmp_pair=Make_pair (newx,maxheight)); -curheight=MaxHeight; -                     } the                 } the             } the         } the         returnRes; -     } the};

The Skyline 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.