CodeTyper

CSES: Convex Hull => Hard

Mar 10th, 2021 (edited)
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  4. #define Bye return 0
  5. #define CodeTyper main
  6. #define ll int long long
  7.  
  8. using namespace std;
  9.  
  10. struct point{
  11.     int x = 0, y = 0;
  12.  
  13.     bool operator == (point other) const{
  14.         return x == other.x && y == other.y;
  15.     }
  16. };
  17.  
  18. int i, j;
  19. point pivot;
  20.  
  21. struct vec{
  22.     int x = 0, y = 0;
  23.     vec(int _x, int _y) : x(_x), y(_y){};
  24. };
  25.  
  26. vec to_vec(point a, point b){
  27.     return vec(b.x-a.x, b.y-a.y);
  28. }
  29.  
  30. long long cross(vec a, vec b){
  31.     return (a.x*1ll*b.y-a.y*1ll*b.x);
  32. }
  33.  
  34. bool counter_clock_wise(point p, point q, point r){
  35.     return cross(to_vec(p, q), to_vec(p, r))>=0;
  36. }
  37.  
  38. long long dist(point a, point b){
  39.     return a.x*1ll*a.x + b.y*1ll*b.y;
  40. }
  41.  
  42. bool collinear(point p, point q, point r){
  43.     return cross(to_vec(p, q), to_vec(q, r)) == 0;
  44. }
  45.  
  46. bool angleCmp(point a, point b){
  47.     if(collinear(pivot, a, b))
  48.         return dist(pivot, a)<dist(pivot, b);
  49.    
  50.     vec d1(a.x - pivot.x, a.y - pivot.y);
  51.     vec d2(b.x - pivot.x, b.y - pivot.y);
  52.  
  53.     return (atan2(d1.y, d1.x)-atan2(d2.y, d2.x))<0;
  54. }
  55.  
  56.  
  57. void solve() {
  58.     ll n; cin>>n;
  59.     vector<point> P(n);
  60.     for (auto& i : P)
  61.         cin>>i.x>>i.y;
  62.    
  63.     if(n<=3){
  64.         cout<<n<<endl;
  65.         for (auto i : P)
  66.             cout<<i.x<<" "<<i.y<<endl;
  67.         return;
  68.     }
  69.  
  70.     j = 0;
  71.     for (i = 0; i<n; i++)
  72.         if(P[i].y<P[j].y || (P[i].y == P[j].y && P[i].x<P[j].x))
  73.             j = i;
  74.  
  75.     swap(P[0], P[j]);
  76.     pivot = P[0];
  77.  
  78.     sort(++P.begin(), P.end(), angleCmp);
  79.  
  80.     vector<point> S;
  81.     S.push_back(P[0]);
  82.     S.push_back(P[1]);
  83.     S.push_back(P[2]);
  84.  
  85.     for (i = 3; i<n; i++){
  86.         j = (int) S.size();
  87.  
  88.         while(j>2){
  89.             if(counter_clock_wise(S[j-2], S[j-1], P[i]))
  90.                 break;
  91.             S.pop_back();
  92.             j--;
  93.         }
  94.        
  95.         S.push_back(P[i]);
  96.     }
  97.  
  98.     set<pair<int, int>> ans;
  99.     for (auto i : S)
  100.         ans.insert({i.x, i.y});
  101.    
  102.     cout<<ans.size()<<endl;
  103.     for (auto i : ans)
  104.         cout<<i.first<<" "<<i.second<<endl;
  105. }
  106.  
  107. int CodeTyper()
  108. {
  109.     boost;
  110.     solve();
  111.     Bye;
  112. }
  113.  
  114. /*
  115.     'Think FIRST, then CODE.'
  116.     'May the CODE be with YOU.'
  117.     'We are PROGRAMMERS, we have NO LIFE!'
  118. */
  119.  
Advertisement
Add Comment
Please, Sign In to add comment