Cod sursa(job #2527233)

Utilizator PatrascuAdrian1Patrascu Adrian Octavian PatrascuAdrian1 Data 19 ianuarie 2020 20:46:17
Problema Infasuratoare convexa Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

ifstream in("infasuratoare.in");
ofstream out("infasuratoare.out");

typedef long double ld;
typedef pair<ld,ld> Point;
const int Nmax = 1e5 + 5 + 1e4 + 5;
Point v[Nmax],s[Nmax];
int p,N,top;

static inline double cross_product(const Point& A, const Point& B, const Point& C) {
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

static inline int cmp(const Point& p1, const Point& p2) {
    return cross_product(v[1], p1, p2) < 0;
}

void sort_points() {
    p = 1;
    for (int i = 2; i <= N; ++i)
        if (v[i] < v[p])
            p = i;
    swap(v[1], v[p]);
    sort(v + 2, v + N + 1, cmp);
}

void convex_hull() {
    sort_points();
    s[1] = v[1],s[2] = v[2];
    top = 2;
    for(int i = 3; i <= N; ++i) {
        while(top >= 2 && cross_product(s[top - 1],s[top],v[i]) > 0)
            top--;
        s[++top] = v[i];
    }
}

void write() {
    //out << fixed <<setprecision(9);
    out << top << '\n';
    for (int i = top; i >= 1; --i)
        out <<  s[i].x << " " << s[i].y << '\n';
}
int main() {
    in >> N;
    for(int i = 1; i <= N; ++i)
        in >> v[i].x >> v[i].y;
    convex_hull();
    write();
    return 0;
}