Cod sursa(job #3177107)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 28 noiembrie 2023 15:49:08
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <fstream>
#include <algorithm>
#include <iomanip>

#define DL long double

using namespace std;

const int Nmax = 120005;
const DL eps = 1e-12;

struct punct{
    DL x;
    DL y;
};

punct v[Nmax];
bool inStiva[Nmax];

int st[Nmax];
int st_index;

void push(int indice){
    st[++st_index] = indice;

    inStiva[indice] = 1;
}
void pop(){
    inStiva[st[st_index]] = 0;
    st_index--;
}

bool cmp(punct a, punct b){
    if(a.x == b.x){
        return a.y < b.y;
    }
    return a.x < b.x;
}

DL angle(punct A, punct B, punct C){
    return ((B.x - A.x) * (C.y - A.y)) - ((C.x - A.x) * (B.y - A.y));
}

int main(){
    ifstream fin("infasuratoare.in");
    ofstream fout("infasuratoare.out");

    int n;

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

    sort(v + 1, v + n + 1, cmp);

    st_index = 0;
    push(1);
    push(2);

    for(int i = 3; i <= n; i++){
        while(st_index > 1 && angle(v[st[st_index - 1]], v[st[st_index]], v[i]) <= eps){
            pop();
        }
        push(i);
    }

    for(int i = n - 1; i >= 1; i--){
        if(!inStiva[i]){
            while(st_index > 1 && angle(v[st[st_index - 1]], v[st[st_index]], v[i]) <= eps){
                pop();
            }
            push(i);
        }
    }

    fout << st_index << '\n';
    fout << setprecision(6) << fixed;
    for(int i = 1; i <= st_index; i++){
        fout << v[st[i]].x << " " << v[st[i]].y << '\n';
    }

    return 0;
}