Cod sursa(job #2775655)

Utilizator bubblegumixUdrea Robert bubblegumix Data 16 septembrie 2021 17:48:23
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 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 bool point_cmp(const pct& a, const pct& b)
{
    return (a.y < b.y || (a.y == b.y && a.x < b.x));
}
inline int cmp(const pct& p1, const pct& p2) {
    
    auto cpd= cross_product(puncte[1], p1, p2);
    if (cpd < 0)
        return true;
    else
        if (cpd > 0)
            return false;
        else
            return p1 > p2;
 
}
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 (point_cmp(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';


}