Raslboyy

Выпуклая оболочка

Apr 8th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. //#pragma GCC optimize("O3")
  4. //#pragma GCC optimize("unroll-loops")
  5.  
  6. #define mp make_pair
  7. #define pb push_back
  8. #define eb emplace_back
  9. #define x first
  10. #define y second
  11. #define sz(x) (int)x.size()
  12. #define all(x) begin(x), end(x)
  13. #define rall(x) rbegin(x), rend(x)
  14. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  15. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  16.  
  17. using namespace std;
  18.  
  19. typedef unsigned long long ull;
  20. typedef long long ll;
  21. typedef long double ld;
  22. typedef pair<int, int> pi;
  23. typedef pair<ld, ld> pld;
  24. typedef pair<ll, ll> pl;
  25. typedef vector<int> veci;
  26. typedef vector<bool> vecb;
  27. typedef vector<vector<int>> vvi;
  28. typedef vector<vector<bool>> vvb;
  29.  
  30. const int INF_I = 1e9;
  31. const ll INF_LL = 1e18;
  32. const int MOD = 1000000007;
  33. const double eps = 1e-6;
  34.  
  35. pld m(pld a, pld b){
  36.     return {b.x - a.x, b.y - a.y};
  37. }
  38.  
  39. ld vec(pld a, pld b){
  40.     return a.x * b.y - a.y * b.x;
  41. }
  42.  
  43. ld l(pld a, pld b){
  44.     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  45. }
  46.  
  47. int main() {
  48.  
  49.     ios::sync_with_stdio(false);
  50.     cin.tie(nullptr);
  51.     cout.tie(nullptr);
  52.  
  53.     int n;
  54.     cin >> n;
  55.     vector<pi> a(n);
  56.     pi mn = {INF_I, INF_I};
  57.     for (int i = 0; i < n; i++) {
  58.         cin >> a[i].x >> a[i].y;
  59.         mn = min(mn, a[i]);
  60.     }
  61.     for (int i = 0; i < n; i++) {
  62.         a[i] = {a[i].x - mn.x, a[i].y - mn.y};
  63.     }
  64.     a.erase(find(all(a), mp(0, 0)));
  65.     sort(all(a), [](pi a1, pi a2){
  66.         return atan2(a1.y, a1.x) < atan2(a2.y, a2.x);
  67.     });
  68.     a.eb(mp(0, 0));
  69.     for (int i = n-1; i >= 1; i--)
  70.         swap(a[i], a[i-1]);
  71.     veci b;
  72.     b.reserve(n);
  73.     b.eb(0), b.eb(1);
  74.     for (int i = 2; i < n; i++){
  75.         pld v1 = m(a[b[sz(b)-2]], a[i]);
  76.         pld v2 = m(a[b[sz(b)-2]], a[b[sz(b)-1]]);
  77.         while (sz(b) >= 2 && vec(v2, v1) < 0) {
  78.             b.pop_back();
  79.             v1 = m(a[b[sz(b)-2]], a[i]);
  80.             v2 = m(a[b[sz(b)-2]], a[b[sz(b)-1]]);
  81.         }
  82.         b.eb(i);
  83.     }
  84.     ld P = 0;
  85.     ld S = 0;
  86.     for (int i = 0; i < sz(b); i++) {
  87.         S += vec(a[b[i]], a[b[(i + 1) % sz(b)]]) * 0.5;
  88.         P += l(a[b[i]], a[b[(i + 1) % sz(b)]]);
  89.     }
  90.     cout << fixed << P << " " << fabs(S) << "\n";
  91.  
  92.     return 0;
  93. }
Add Comment
Please, Sign In to add comment