Advertisement
Guest User

otoczka wypukla

a guest
Jan 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define x first
  5. #define y second
  6. const int maxn = 1e6+6;
  7. pair<int,int> punkty[2*maxn];
  8.  
  9. bool cmp(pair<int,int> a, pair<int,int> b){
  10.     return a.y<b.y;
  11. }
  12. int iloczyn(int x0, int y0, int x1, int y1, int x2, int y2){
  13.     return ((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0));
  14. }
  15. vector<int> stos;
  16.  
  17. int main()
  18. {
  19.     int n;
  20.     cin >> n;
  21.     for (int i=1; i<=n; i++)
  22.         cin >> punkty[i].x >> punkty[i].y;
  23.     sort(punkty+1, punkty+1+n, cmp);
  24.     int nr = 1;
  25.     for (int i=2*n-1; i>n; i--){
  26.         punkty[i].x=punkty[nr].x;
  27.         punkty[i].y=punkty[nr].y;
  28.         nr++;
  29.     }
  30.     stos.push_back(1);
  31.     stos.push_back(2);
  32.     int ost = 1;
  33.     for (int i=3; i<=2*n-1; i++)
  34.     {
  35.         int kon = stos[ost], przed = stos[ost-1];
  36.         int ilo = iloczyn(punkty[przed].x, punkty[przed].y, punkty[kon].x, punkty[kon].y, punkty[i].x, punkty[i].y);
  37.         while(ilo>0 && ost > 1)
  38.         {
  39.             stos.pop_back();
  40.             ost--;
  41.             kon = stos[ost], przed = stos[ost-1];
  42.             ilo = iloczyn(punkty[przed].x, punkty[przed].y, punkty[kon].x, punkty[kon].y, punkty[i].x, punkty[i].y);
  43.         }
  44.         if (ilo>0)
  45.         {
  46.             stos.pop_back();
  47.             ost--;
  48.         }
  49.         stos.push_back(i);
  50.         ost++;
  51.     }
  52.     stos.pop_back();
  53.     cout << "STOS:\n";
  54.     for (int i=0; i<stos.size(); i++)
  55.         cout << punkty[stos[i]].x << " " << punkty[stos[i]].y << endl;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement