Time Limit: 3.000 seconds
Background
With the advent of high speed graphics workstations, CAD (computer-aided design) and other areas (cam, mass design) have made increasingly restricted tive use of computers. one of the problems with drawing images is the elimination of hidden lines -- lines obscured by other parts of a drawing.
The Problem
You are to design a program to assist an impact ect in drawing the skyline of a city given the locations of the buildings in the city. to make the problem tractable, all buildings are rectangular in shape and they share a common bottom (the city they are built in is very flat ). the city is also viewed as two-dimen.pdf. A building is specified by an ordered triple (Li, hi, RI) where Li and RI are left and right coordinates, respectively, of building I and HI is the height of the building. in the dimo-below buildings are shown on the left with triples (, 5), (, 7), (, 9), (, 16, 25), (, 22), (, 29), (, 28)
The skyline, shown on the right, is represented by the sequence: (1, 11, 3, 13, 9, 0, 12, 7, 16, 3, 19, 18, 22, 3, 23, 13, 29, 0)
The input
The input is a sequence of building triples. all coordinates of buildings are positive integers less than 10,000 and there will be at least one and at most 5,000 buildings in the input file. each building triple is on a line by itself in the input file. all integers in a triple are separated by one or more spaces. the triples will be sorted by Li, the left X-coordinate of the building, so the building with the smallest left X-coordinate is first in the input file.
The output
The output shoshould consist of the vector that describes the skyline as shown in the example above. in the skyline vector (V1, V2, V3 ,..., vn-2, vn-1, NV), the VI such that I is an even number represent a horizontal line (height ). the VI such that I is an odd number represent a vertical line (X-coordinate ). the skyline vector shoshould represent the "path" taken, for example, by a bug starting at the minimum X-coordinate and traveling horizontally and vertically over all the lines that define the skyline. thus the last entry in the skyline vector will be a 0. the coordinates must be separated by a blank space.
Sample Input
1 11 5
2 6 7
3 13 9
12 7 16
14 3 25
19 18 22
23 13 29
24 4 28
Sample output
1 11 3 13 9 0 12 7 16 3 19 18 22 3 23 13 29 0
Hint
This is a simple question. Because the limited data volume is small, you can open an array of horizontal coordinates to directly calibrate the height of each point. However, it is worth thinking about how to use as little memory and time as possible. My idea is to divide the image into small sections from left to right based on the input data. This way, you can input and process the image simultaneously without storing the data of the entire coordinate axis. When a new block is entered, the relationship between the block and the existing segment is determined from the right to the left, and corresponding insert and update operations are performed. When repeating each existing section, make sure that the new block is right aligned with the new section. If there is no alignment, break down and insert it. After each round of processing, the new segment is reduced to the right of the next segment.
Analysis
# Include <iostream >#include <list> using namespace STD; struct outline {int nleft; int nright; int nheight ;}; int main (void) {outline curol, newol; list <Outline> listskyline; CIN> curol. nleft> curol. nheight> curol. nright; listskyline. push_front (curol); While (CIN> curol. nleft> curol. nheight> curol. nright) {If (curol. nheight <= listskyline. front (). nheight & curol. nright <= listskyline. front (). nrig HT) {continue;} If (curol. nleft> listskyline. front (). nright) {newol. nleft = listskyline. front (). nright; newol. nright = curol. nleft; newol. nheight = 0; listskyline. push_front (newol);} For (list <outline >:: iterator iprev = listskyline. begin (); iprev! = Listskyline. end (); ++ iprev) {If (curol. nleft = curol. nright) {break;} If (curol. nright <= iprev-> nleft) {continue;} If (curol. nright> iprev-> nright) {newol. nleft = iprev-> nright; newol. nright = curol. nright; newol. nheight = curol. nheight; iprev = listskyline. insert (iprev, newol); curol. nright = newol. nleft; continue;} If (curol. nright <iprev-> nright & curol. nheight> iprev-> nheight) {newol. nleft = cur Ol. nright; newol. nright = iprev-> nright; newol. nheight = iprev-> nheight; iprev-> nright = newol. nleft; iprev = listskyline. insert (iprev, newol); continue;} If (curol. nleft> iprev-> nleft) {If (curol. nheight> iprev-> nheight) {iprev-> nright = curol. nleft; listskyline. insert (iprev, curol);} break;} If (curol. nheight> iprev-> nheight) {iprev-> nheight = curol. nheight;} curol. nright = iprev-> nleft;} int nlastheig Ht = 0; For (list <Outline>: reverse_iterator rI = listskyline. rbegin (); Ri! = Listskyline. rend (); ++ RI) {If (ri-> nheight! = Nlastheight) {cout <ri-> nleft <''<ri-> nheight <''; nlastheight = ri-> nheight ;}} cout <listskyline. front (). nright <"0" <Endl; return 0 ;}