Kaidul

Untitled

May 11th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <list>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. class Solution {
  10.     void merge(vector<pair<int, int>>& lhs, vector<pair<int, int>>& rhs, vector<pair<int, int>>& result) {
  11.         int m = (int)lhs.size();
  12.         int n = (int)rhs.size();
  13.        
  14.         int i = 0, j = 0;
  15.         while(i < m and j < n) {
  16.             int k = (int)result.size() - 1;
  17.             if(lhs[i] < rhs[j]) {
  18.                
  19.                 if(result.empty() or result[k].first < lhs[i].first) {
  20.                     result.push_back(lhs[i++]);
  21.                     if(i < m and lhs[i].first - lhs[i - 1].first > 0) {
  22.                         result.push_back({lhs[i].first - 1, lhs[i - 1].second});
  23.                     }
  24.                 }
  25.                 else if(lhs[i].first == result[k].first) {
  26.                     result[k].second = max(result[k].second, lhs[i].second);
  27.                     ++i;
  28.                     if(i < m and lhs[i].first - lhs[i - 1].first > 0) {
  29.                         result.push_back({lhs[i].first - 1, lhs[i - 1].second});
  30.                     }
  31.                 }
  32.                 else if(lhs[i].first < result[k].first) {
  33.                     if(lhs[i].second <= result[k].second) {
  34.                         ++i;
  35.                         if(i < m and lhs[i].first - lhs[i - 1].first > 0 and lhs[i].first - 1 > result[k].first) {
  36.                             result.push_back({result[k].first, lhs[i - 1].second});
  37.                             result.push_back({lhs[i].first - 1, lhs[i - 1].second});
  38.                         }
  39.                     }
  40.                     else {
  41.                         pair<int, int> elem = result[k];
  42.                         result[k] = lhs[i++];
  43.                         if(i < m and lhs[i].first - lhs[i - 1].first > 0) {
  44.                             if(lhs[i].first - 1 < elem.first) {
  45.                                 result.push_back({lhs[i].first - 1, elem.second});
  46.                                 result.push_back(elem);
  47.                             }
  48.                             else if(lhs[i].first - 1 > elem.first) {
  49.                                 result.push_back({lhs[i].first - 1, lhs[i - 1].second});
  50.                             }
  51.                             else {
  52.                                 result.push_back({lhs[i].first - 1, lhs[i - 1].second});
  53.                             }
  54.                         }
  55.                     }
  56.                 }
  57.                
  58.             }
  59.             else {
  60.                
  61.                 if(result.empty() or result[k].first < rhs[j].first) {
  62.                     result.push_back(rhs[j++]);
  63.                     if(j < n and rhs[j].first - rhs[j - 1].first > 0) {
  64.                         result.push_back({rhs[j].first - 1, rhs[j - 1].second});
  65.                     }
  66.                 }
  67.                 else if(rhs[j].first == result[k].first) {
  68.                     result[k].second = max(result[k].second, rhs[j].second);
  69.                     ++j;
  70.                     if(j < n and rhs[j].first - rhs[j - 1].first > 0) {
  71.                         result.push_back({rhs[j].first - 1, rhs[j - 1].second});
  72.                     }
  73.                 }
  74.                 else if(rhs[j].first < result[k].first) {
  75.                     if(rhs[j].second <= result[k].second) {
  76.                         ++j;
  77.                         if(j < n and rhs[j].first - rhs[j - 1].first > 0 and rhs[j].first - 1 > result[k].first) {
  78.                             result.push_back({result[k].first, rhs[j - 1].second});
  79.                             result.push_back({rhs[j].first - 1, rhs[j - 1].second});
  80.                         }
  81.                     }
  82.                     else {
  83.                         pair<int, int> elem = result[k];
  84.                         result[k] = rhs[j++];
  85.                         if(j < n and rhs[j].first - rhs[j - 1].first > 0) {
  86.                             if(rhs[j].first - 1 < elem.first) {
  87.                                 result.push_back({rhs[j].first - 1, elem.second});
  88.                                 result.push_back(elem);
  89.                             }
  90.                             else if(rhs[j].first - 1 > elem.first) {
  91.                                 result.push_back({rhs[j].first - 1, rhs[j - 1].second});
  92.                             }
  93.                             else {
  94.                                 result.push_back({rhs[j].first - 1, rhs[j - 1].second});
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.                
  100.             }
  101.         }
  102.        
  103.         while(i < m) {
  104.             result.push_back(lhs[i++]);
  105.         }
  106.         while(j < n) {
  107.             result.push_back(rhs[j++]);
  108.         }
  109.     }
  110.    
  111.     void compress(vector<pair<int, int>>& result) {
  112.         vector<pair<int, int>> tmp;
  113.         int n = (int)result.size();
  114.         for(int i = 0; i < n; ++i) {
  115.             int height = result[i].second;
  116.             tmp.push_back(result[i]);
  117.             while(i + 1 < n and result[i + 1].second == height) {
  118.                 ++i;
  119.             }
  120.         }
  121.         result.clear();
  122.         result.insert(result.end(), tmp.begin(), tmp.end());
  123.     }
  124.    
  125.     vector<pair<int, int>> getSkyline(int left, int right, vector<vector<int>> const& buildings) {
  126.         if(left == right) {
  127.             vector<pair<int, int>> result;
  128.             result.push_back({buildings[left][0], buildings[left][2]});
  129.             if(buildings[left][2] > 0) {
  130.                 result.push_back({buildings[left][1], 0});
  131.             }
  132.             return result;
  133.         }
  134.         int mid = left + (right - left) / 2;
  135.         vector<pair<int, int>> leftline = getSkyline(left, mid, buildings);
  136.         vector<pair<int, int>> rightline = getSkyline(mid + 1, right, buildings);
  137.        
  138.         vector<pair<int, int>> result;
  139.         merge(leftline, rightline, result);
  140.         compress(result);
  141.        
  142.         return result;
  143.     }
  144.    
  145. public:
  146.     vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
  147.         vector<pair<int, int>> result;
  148.         if(buildings.empty()) return result;
  149.        
  150.         sort(buildings.begin(), buildings.end());
  151.         int n = (int)buildings.size();
  152.         result = getSkyline(0, n - 1, buildings);
  153.        
  154.         return result;
  155.     }
  156. };
  157.  
  158.  
  159.  
  160. int main(void) {
  161.     vector<vector<int>> v{{2, 9, 10}, {3, 7, 15}, {5, 12, 12}, {15, 20, 10}, {19, 24, 8}};
  162.     vector<pair<int, int>> result = Solution().getSkyline(v);
  163.     return 0;
  164. }
Add Comment
Please, Sign In to add comment