Cod sursa(job #2775627)

Utilizator bubblegumixUdrea Robert bubblegumix Data 16 septembrie 2021 16:37:10
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include<iostream>
#include<stack>
#include<iomanip>
#include<fstream>
#include<algorithm>
#define x first
#define y second
#define nmax 120005
using namespace std;
typedef pair<double, double> pct;
pct puncte[nmax];
pct hull[nmax];
int head;
int n;

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

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

inline int cmp(const pct& p1, const pct& p2) {
    return cross_product(puncte[1], p1, p2) < 0;
}
void convex_hull()
{
    hull[1] = puncte[1];
    hull[2] = puncte[2];
    head = 2;
    for (int i = 3; i <= n; i++)
    {
        while (head >= 2 && (cross_product(hull[head-1],hull[head],puncte[i]) > 0))
            --head;
        hull[++head] = puncte[i];

    }
}

int main()
{
    f >> n;
    for (int i = 1; i <= n; i++)
        f >> puncte[i].x >> puncte[i].y;
    int pos = 1;
    for (int i = 2; i <= n; i++)
        if (puncte[i] < puncte[pos])
            pos = i;
    swap(puncte[1], puncte[pos]);
    sort(puncte + 2, puncte + n + 1,cmp);

    convex_hull();
    g << fixed;
    g << head << '\n';
    for (int i = head; i >= 1; i--)
        g << setprecision(9)<< hull[i].x <<" "<< hull[i].y << '\n';


}