Cod sursa(job #2266660)

Utilizator BogdanisarBurcea Bogdan Madalin Bogdanisar Data 22 octombrie 2018 20:21:06
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.2 kb
#include <bits/stdc++.h>
  
using namespace std;
  
#if 1
    #define pv(x) cout<<#x<<" = "<<(x)<<"; ";cout.flush()
    #define pn cout<<endl
#else
    #define pv(x)
    #define pn
#endif
  
// #if 1
#ifdef INFOARENA
    ifstream in("infasuratoare.in");
    ofstream out("infasuratoare.out");
#else
    #define in cin
    #define out cout
#endif
  
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pld = pair<ld, ld>;
#define pb push_back
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
const int inf_int = 1e9 + 5;
const ll inf_ll = 1e18 + 5;
const int NMax = 2e2 + 5;
const int mod = 1e9 + 7;
const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0};

template<typename T>
T orientation(pair<T,T> a, pair<T,T> b, pair<T,T> c) {
    T ans = a.first * b.second + b.first * c.second + c.first * a.second;
    ans -= c.first * b.second + b.first * a.second + a.first * c.second;
    return ans;
}

template<typename T>
vector<pair<T,T>> jarvisMarch(vector<pair<T,T>> v) {
    pair<T,T> mn = {1e18, 1e18};
    for (auto p : v) {
        mn = min(mn, p);
    }
    
    vector<pair<T,T>> ans;
    ans.pb(mn);
    pair<T,T> currPoint = mn;
    while (true) {
        pair<T,T> rightmost = (v[0] == currPoint) ? v[1] : v[0];
        for (auto p : v) {
            if (p == currPoint || p == rightmost) {
                continue;
            }

            if (orientation(currPoint, rightmost, p) < 0) {
                rightmost = p;
            }
        }

        if (rightmost == ans[0]) {
            break;
        }
        ans.pb(rightmost);
        currPoint = rightmost;
    }

    return ans;
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    
    int N;
    in >> N;
    vector<pld> v(N);
    for (auto& p : v) {
        in >> p.first >> p.second;
    }

    vector<pld> ans = jarvisMarch(v);

    out << ans.size() << '\n';
    out << fixed << setprecision(14);
    for (auto p : ans) {
        out << p.first << ' ' << p.second << '\n';
    }
    
    return 0;
}