【topic】
Given a three-dimensional array of N rows, each row represents a building has a total of N Block building.
All buildings are located at the bottom of the X-axis, three values for each row (a, b, c) on behalf of the (a, 0) point on each building, and the (b, 0) the end point, the height is c .
The input data can ensure that a <b, and a, b, c are positive. There may be overlap between the buildings. Please outline of the overall output line.
Examples: Given a two-dimensional array [[1, 3, 3], [2, 4, 4], [5, 6, 1]]
Output contour [[1, 2, 3], [2, 4, 4], [5, 6, 1]]
【answer】
[1, 2, 3] indicates the height of the first number to the second number is 3
[2, 4, 4] represents the height of the second number is the number of 4 4
The array of decomposition:
[1, 2, 3] is decomposed into: [1, 3, upper] [3, 3, lower]
These arrays are then sorted by decomposing a first number ascending
Prepare a red-black tree map, key value is the number of story that appears as a value story
How to know whether an outline of it?
Starting initialization storey 0
When pressed by the upper data, the key values of the respective storey value ++
Each pressed into a digital, on the map to view the highest value in the story [that is] key map in the biggest change has occurred,
Once changed, the profile is generated, either up or down
No change is no profile
When press-fitting the data, the value of the corresponding key value storey -, value == 0 key record delete article
note
When two records generate the same location, such as a next data, and data on a position of the 4, you sort the data in the pretreatment, before whom who after?
Is typically labeled by the top surface, because the change is recorded as the next story, the previous story must be changed on
When press-fitting the data to be recorded the maximum number of height positions per
[Code]
. 1 #pragma Once 2 #include <the iostream> . 3 #include <Map> . 4 #include <Vector> . 5 #include <algorithm> . 6 . 7 the using namespace STD; . 8 . 9 Vector <Vector < int >> printContour (Vector <Vector < int >> V) 10 { . 11 Vector <Vector < int >> data; // data stored preprocessing 12 is Vector <Vector < int >> RES; // answer 13 is Map < int ,int> MH; // record storey 14 Map < int , int > MP; // recording position 15 for (Auto & A : V) 16 { . 17 Vector < int > NUM ( . 3 , 0 ); 18 is NUM [ 0 ] = A [ 0 ]; // start point . 19 NUM [ . 1 ] = A [ 2 ]; // Height 20 is NUM [ 2 ] = . 1 ; // story marked on 21 data.push_back (NUM); 22 is NUM [ 0 ] = A [ . 1 ]; // end point 23 is NUM [ . 1 ] = A [ 2 ]; // storey 24 NUM [ 2 ] = 0 ; // storey tag for the next 25 data.push_back (NUM); 26 is } 27 Sort (data.begin (), data.end (), [] (Vector < int > V1, Vector < int > V2) { return V1 [ 0 ] == v2 [ 0 ] v1 [? 2 ] == 0 : v1 [0 ] <V2 [ 0 ];}); 28 // collation, the same as those of the first digit, downstairs front row are not identical, the small top surface 29 for (Auto & A: Data) 30 { 31 is IF (A [ 2 ]) // F marked on 32 { 33 is IF (mh.find (A [ . 1 ])! = mh.end ()) // the presence storey 34 is ++ MH [A [ . 1 ] ]; // this story exists for increasing the recording 35 the else 36 MH [A [ . 1 ]] = . 1 ; // the story does not exist, adding 37 } 38 is the else // marked as downstairs 39 { 40 IF (mh.find (A [ . 1 ])! = Mh.end ()) // the presence storey 41 is { 42 is IF (MH [A [ . 1 ]] = = 1 ) // this is only a record storey 43 is mh.erase (a [ 1 ]); // delete this record 44 is the else 45 --mh [a [ 1 ]]; // the Save a recording storey 46 } 47 } 48 49 IF(mh.empty ()) // when the story is recorded as 0, i.e., the middle position exists no floor 50 MP [A [ 0 ]] = 0 ; // if this position is marked storey 0 51 is the else 52 is MP [a [ 0 ]] = (--mh.end ()) -> First; // is present with the highest floor building where a recording 53 is } 54 is 55 int Start, height; 56 is Start = height = 0 ; 57 is for ( A & Auto: MP) // through the records 58 { 59 int P = a.first; // position 60 int H = a.second;// storey 61 is IF (! H = height) // change occurs story, then produces a profile of floor 62 is { 63 is IF (height =! 0 ) 64 { 65 Vector < int > TEMP ( . 3 , 0 ); 66 TEMP [ 0 ] = Start; 67 TEMP [ . 1 ] = P; 68 TEMP [ 2 ] = height; 69 res.push_back (TEMP); 70 } 71 is Start = P; // re-record 72 height = H; 73 is } 74 } 75 76 return RES; 77 } 78 79 80 void the Test () 81 { 82 Vector <Vector < int >> V; 83 V = {{ . 1 , 3 , 3 }, { 2 , 4 , 4 }, { 5 , 6 , 1 }}; 84 v = printContour(v); 85 for (auto &a : v) 86 { 87 for (auto b : a) 88 cout << b << " "; 89 cout << endl; 90 } 91 cout << "///////////////////////////////////" << endl; 92 v = { {1,5,4}, {2,3,3}, {4,7,1},{6,9,5},{8,10,2}}; 93 v = printContour(v); 94 for (auto &a : v) 95 { 96 for (auto b : a) 97 cout << b << " "; 98 cout << endl; 99 } 100 cout << "///////////////////////////////////" << endl; 101 v = { {1,3,3}, {2,4,4}, {5,6,1} }; 102 v = printContour(v); 103 for (auto &a : v) 104 { 105 for (auto b : a) 106 cout << b << " "; 107 cout << endl; 108 } 109 cout << "///////////////////////////////////" << endl; 110 v = { {1,4,2},{2,9,1},{3,6,3},{5,8,4},{7,10,5}}; 111 v = printContour(v); 112 for (auto &a : v) 113 { 114 for (auto b : a) 115 cout << b << " "; 116 cout << endl; 117 } 118 cout << "///////////////////////////////////" << endl; 119 120 }