Advertisement
yungyao

Convex Hull

Mar 3rd, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. using namespace std;
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <utility>
  7. #include <bitset>
  8. #include <set>
  9. #include <string>
  10. #include <iomanip>
  11. #include <map>
  12.  
  13. #define pb push_back
  14. #define pii pair<int,int>
  15. #define F first
  16. #define S second
  17. #define LL long long
  18. #define mid (LB+RB)/2
  19. #define iter(x) x.begin(),x.end()
  20.  
  21. /*
  22. 8e7 so dian
  23. FHVirus so dian
  24. youou so dian
  25. KYW so dian
  26. hubert so dian
  27. jass so dian
  28. tingyu so dian
  29. */
  30.  
  31. //IO
  32. #include <iostream>
  33. #define theyRSOOOOOOOOODIAN ios_base::sync_with_stdio(false),cin.tie(0);
  34. #define endl '\n'
  35.  
  36. //workspace
  37.  
  38. #define x first
  39. #define y second
  40. inline bool cross(pii o,pii a,pii b){
  41.     return (a.x - o.x) * (b.y - o.y) <= (a.y - o.y) * (b.x - o.x);
  42. }
  43.  
  44. struct stack{
  45.     pii data[100100];
  46.     int p = 0;
  47.     inline void push(pii x){
  48.         data[p++] = x;
  49.     }
  50.     inline pii top(){
  51.         return data[p-1];
  52.     }
  53.     inline pii sectop(){
  54.         return data[p-2];
  55.     }
  56.     inline bool pop(){
  57.         if (p){
  58.             --p;
  59.             return 1;
  60.         }
  61.         return 0;
  62.     }
  63.     inline int unsigned size(){
  64.         return p;
  65.     }
  66. };
  67.  
  68. int main(){
  69.     theyRSOOOOOOOOODIAN
  70.     int n;
  71.     vector <pii> v;
  72.  
  73.     cin >> n;
  74.     while (n--){
  75.         int X,Y;
  76.  
  77.         cin >> X >> Y;
  78.         v.pb({X,Y});
  79.     }
  80.     sort (iter(v));
  81.  
  82.     stack ch;
  83.     for (auto i:v){
  84.         while (ch.size() >= 2 && cross(ch.sectop(),ch.top(),i))
  85.             ch.pop();        
  86.  
  87.         ch.push(i);
  88.     }
  89.    
  90.     for (int i=v.size()-2,lm=ch.size()+1;i>=0;--i){
  91.         while (ch.size() >= lm && cross(ch.sectop(),ch.top(),v[i]))
  92.             ch.pop();
  93.  
  94.         if (i)
  95.             ch.push(v[i]);
  96.     }
  97.  
  98.     cout << ch.size();
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement