Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. static int optimize = []() {ios::sync_with_stdio(false); cin.tie(nullptr); return 0;} ();
  2.  
  3. using ipair = pair<int,int>;
  4.  
  5. namespace std{
  6.     template<>
  7.     struct hash<ipair> {
  8.         size_t operator()(const ipair &p) const {
  9.             return p.first*31 ^ (p.second*127+8989);
  10.         }
  11.     };
  12. }
  13.  
  14. class Solution {
  15. public:
  16.     bool isRectangleCover(vector<vector<int>>& rec) {
  17.         int ymin = INT_MAX;
  18.         int xmin = INT_MAX;
  19.         int ymax = 0;
  20.         int xmax = 0;
  21.         int64_t sq = 0;
  22.        
  23.         unordered_map<ipair, int> points;
  24.         for (int i = 0; i < rec.size(); ++i) {
  25.             ymin = min(min(ymin, rec[i][0]), rec[i][2]);
  26.             xmin = min(min(xmin, rec[i][1]), rec[i][3]);
  27.             ymax = max(max(ymax, rec[i][0]), rec[i][2]);
  28.             xmax = max(max(xmax, rec[i][1]), rec[i][3]);
  29.            
  30.             vector<int> py = {rec[i][0], rec[i][0], rec[i][2], rec[i][2]};
  31.             vector<int> px = {rec[i][1], rec[i][3], rec[i][1], rec[i][3]};
  32.             for (int j = 0; j < 4; ++j) {
  33.                 points[{py[j], px[j]}]++;
  34.             }
  35.             sq += (rec[i][2]-rec[i][0])*(rec[i][3]-rec[i][1]);
  36.         }
  37.        
  38.         if (sq != (ymax-ymin)*(xmax-xmin)) return false;        
  39.        
  40.         int angle_count = 0;
  41.         for (auto x: points) {
  42.             int count = x.second;
  43.             ipair point = x.first;
  44.             switch (count)  {
  45.                 case 1:
  46.                     angle_count++;
  47.                     if ((point.first != ymin && point.first != ymax) || (point.second != xmin && point.second != xmax)) {
  48.                         return false;
  49.                     }
  50.                     break;
  51.                 case 2:
  52.                     break;                    
  53.                 case 4:
  54.                     if (point.first == ymin || point.first == ymax || point.second == ymin || point.second == ymax) {
  55.                         return false;
  56.                     }
  57.                     break;
  58.                 default:
  59.                     return false;
  60.             }
  61.         }
  62.        
  63.         return angle_count == 4;
  64.        
  65.     }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement