Advertisement
Guest User

704.cpp

a guest
Jun 24th, 2018
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cassert>
  6.  
  7. struct Point {
  8.     int x, y;
  9.    
  10.     inline int norm2() const {
  11.         return x * x + y * y;
  12.     }
  13. };
  14.  
  15. inline int cross(Point a, Point b) {
  16.     return a.x * b.y - a.y * b.x;
  17. }
  18.  
  19. bool solve(int side, std::vector<Point> p) {
  20.    
  21.     std::vector<Point> positive, negative;
  22.    
  23.     for (auto& it : p) {
  24.         it.x = 2 * it.x - side;
  25.         it.y = 2 * it.y - side;
  26.         if (it.x == 0 && it.y == 0) {
  27.             return false;
  28.         }
  29.         if (it.x > 0 || (it.x == 0 && it.y > 0)) {
  30.             positive.push_back(Point{it.x, it.y});
  31.         } else {
  32.             negative.push_back(Point{it.x, it.y});
  33.         }
  34.     }
  35.     // Сортировка положительной половины и удаление повторяющихся углов:
  36.     std::sort(positive.begin(), positive.end(), [](const Point& a, const Point& b) {
  37.         return a.y * b.x < b.y * a.x || (a.y * b.x == b.y * a.x && a.norm2() < b.norm2());
  38.     });
  39.    
  40.     positive.erase(std::unique(positive.begin(), positive.end(), [](const Point& a, const Point& b) {
  41.         return a.y * b.x == b.y * a.x;
  42.     }), positive.end());
  43.    
  44.     // Сортировка отрицательной половины и удаление повторяющихся углов:
  45.     std::sort(negative.begin(), negative.end(), [](const Point& a, const Point& b) {
  46.         return a.y * b.x < b.y * a.x || (a.y * b.x == b.y * a.x && a.norm2() < b.norm2());
  47.     });
  48.    
  49.     negative.erase(std::unique(negative.begin(), negative.end(), [](const Point& a, const Point& b) {
  50.         return a.y * b.x == b.y * a.x;
  51.     }), negative.end());
  52.    
  53.     // Объединение двух половин точек:
  54.     p.clear();
  55.     p.insert(p.end(), positive.begin(), positive.end());
  56.     p.insert(p.end(), negative.begin(), negative.end());
  57.    
  58.     if (p.size() == 1u) {
  59.         return true;
  60.     }
  61.    
  62.     const int n = p.size();
  63.    
  64.     p.push_back(p[0]);
  65.    
  66.     for (int i = 1; i <= n; ++i) {
  67.         if (cross(p[i-1], p[i]) < 0) {
  68.             return true;
  69.         }
  70.     }
  71.     return false;
  72. }
  73.  
  74. int main() {
  75.    
  76.     int side, n;
  77.     scanf("%d %d", &side, &n);
  78.    
  79.     std::vector<Point> p;
  80.     for (int i = 0; i < n; ++i) {
  81.         int x, y;
  82.         scanf("%d %d", &x, &y);
  83.         assert(0 < x && x < side);
  84.         assert(0 < y && y < side);
  85.         p.push_back(Point{x,y});
  86.     }
  87.     printf(solve(side, p) ? "YES\n" : "NO\n");
  88.    
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement