Cod sursa(job #3345585)

Utilizator EricDimiCismaru Eric-Dimitrie EricDimi Data 10 martie 2026 09:58:08
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
/// Scanarea Graham
#include <fstream>
#include <iomanip>
#include <algorithm>

using namespace std;

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

const int NMAX = 120000;

struct Point {
    double x, y;
};

Point v[NMAX + 1];
int n;

double Area(Point p1, Point p2, Point p3) {
    return p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y);
}

inline bool Comp(Point p1, Point p2) {
    return Area(v[1], p1, p2) > 0;
}

Point st[NMAX + 1];
int vf;

void Read()
{
    f >> n;
    for(int i = 1; i <= n; i++)
        f >> v[i].x >> v[i].y;
}

void Sort_Points()
{
    int ind = 1;
    for(int i = 2; i <= n; i++)
        if(v[i].x < v[ind].x || (v[i].x == v[ind].x && v[i].y < v[ind].y))
            ind = i;
    swap(v[1], v[ind]);
    sort(v + 2, v + n + 1, Comp);
}

void Convex_Hull()
{
    Sort_Points();
    st[++vf] = v[1];
    st[++vf] = v[2];
    for(int i = 3; i <= n; i++)
    {
        while(vf >= 2 && Area(st[vf - 1], st[vf], v[i]) < 0)
            vf--;
        st[++vf] = v[i];
    }
}

void Write()
{
    g << vf << '\n';
    for(int i = 1; i <= vf; i++)
        g << fixed << setprecision(12) << st[i].x << ' ' << st[i].y << '\n';
}

int main()
{
    Read();
    Convex_Hull();
    Write();
    f.close();
    g.close();
    return 0;
}