Cod sursa(job #2537921)

Utilizator MichaelXcXCiuciulete Mihai MichaelXcX Data 4 februarie 2020 09:19:07
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

#define in  "infasuratoare.in"
#define out "infasuratoare.out"

using namespace std;

struct Point {
    double x, y;

    bool operator<(const Point &a) {
        return a.x < x;
    }
};

const int NMAX = 120005;
const double EPS = 1e-12;
Point points[NMAX];
int st[NMAX], n, head;
bool marked[NMAX];

inline double crossProd(Point, Point, Point);
void ConvexHull();

int main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL), cout.tie(NULL);

    freopen(in, "r", stdin);
    freopen(out, "w", stdout);

    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> points[i].x >> points[i].y;

    ConvexHull();
}

inline double crossProd(Point O, Point A, Point B) {
    return (A.x - O.x) * (B.y - O.y) -  (B.x - O.x) * (A.y - O.y);
}

void ConvexHull()
{
    make_heap(points + 1, points + n + 1);
    sort_heap(points + 1, points + n + 1);

    st[1] = 1, st[2] = 2, head = 2;
    marked[2] = true;

    for(int i = 1, p = 1; i > 0; i += (p = (i == n ? -p : p))) {
        if(marked[i])
            continue;

        while(head >= 2 && crossProd(points[st[head - 1]], points[st[head]], points[i]) < EPS)
            marked[st[head--]] = false;

        st[++head] = i;
        marked[i] = true;
    }

    cout << head - 1 << "\n";
    cout << setprecision(6) << fixed;
    for(int i = 1; i < head; ++i)
        cout << points[st[i]].x << " " << points[st[i]].y << "\n";

}