Cod sursa(job #2775622)

Utilizator bubblegumixUdrea Robert bubblegumix Data 16 septembrie 2021 16:24:22
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include<iostream>
#include<stack>
#include<iomanip>
#include<fstream>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<double, double> pct;
pct puncte[1000];
pct hull[1000];
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.precision(6);
    for (int i = head; i >= 1; i--)
        g << hull[i].x * 1.000000 <<" "<< hull[i].y * 1.000000 << '\n';


}