Advertisement
999ms

Untitled

Jun 15th, 2021
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. /*
  2. Дан массив точек, проверить, есть ли вертикальная прямая, относительно которой эти наборы зеркальны
  3.  std::vector<std::pair<int,int>> points;
  4. */
  5.  
  6. bool is_mirror(std::vector<std::pair<int,int>>& arr) {
  7.     const int n = arr.size();
  8.    
  9.     if (n == 0) {
  10.         return true;
  11.     }
  12.  
  13.     int min_x = arr[0].first;
  14.     int max_x = arr[0].first;
  15.    
  16.     for (const auto& [x, y] : arr) {
  17.         min_x = std::min(min_x, x);
  18.         max_x = std::max(max_x, x);
  19.     }
  20.    
  21.     int mid_x = (min_x + max_x);
  22.    
  23.     std::unordered_map<std::pair<int,int>, int> map;
  24.    
  25.     for (const auto& p : arr) {
  26.         if (p.first * 2 == mid_x) {
  27.             continue;
  28.         } else {
  29.             if (p.first * 2 < mid_x) {
  30.                 map[p]++;
  31.             } else {
  32.                 map[{mid - p.first, p.second}]--;
  33.             }
  34.         }
  35.     }
  36.    
  37.     for (auto& [key, value] : map) {
  38.         if (value != 0) {
  39.             return false;
  40.         }
  41.     }
  42.     return true;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement