Cod sursa(job #3273778)

Utilizator stefan_dore_Stefan Dore stefan_dore_ Data 3 februarie 2025 20:12:49
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
using namespace std;

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

const double EPS = 1e-9;
const int NMAX = 120000;

struct punct {
    double x, y;
};

int N, H;
punct P[NMAX+2];
int S[NMAX+2];
bool viz[NMAX+2];

double det(const punct &A, const punct &B, const punct &C) {
    return (A.x - B.x) * (B.y - C.y) - (A.y - B.y) * (B.x - C.x);
}

bool compx(const punct &A, const punct &B) {
    if (A.x == B.x) return A.y < B.y;
    return A.x < B.x;
}

void citire() {
    f >> N;
    for (int i=1; i<=N; i++)
        f >> P[i].x >> P[i].y;
}

void inf_convex() {
    sort(P+1, P+N+1, compx);
    S[1] = 1;
    S[2] = 2;
    H = 2;
    viz[2] = 1;
    for (int i=3, p = 1; i>=1; i+=p) {
        if(viz[i]) continue;
        while(H >= 2 && det(P[S[H-1]], P[S[H]], P[i]) < EPS)
            viz[S[H--]] = 0;
        S[++H] = i;
        viz[i] = 1;
        if (i==N) p = -1;
    }
    H--;
}

void afisare() {
    g << H << '\n';
    g << fixed << setprecision(6);
    for(int i = 1; i<=H; i++)
        g << P[S[i]].x << ' ' << P[S[i]].y << '\n';
}

int main()
{
    citire();
    inf_convex();
    afisare();
    f.close();
    g.close();
    return 0;
}