Cod sursa(job #2541076)

Utilizator ioanaa_ghGhiorghi Ioana-Cristina ioanaa_gh Data 8 februarie 2020 08:49:59
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

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

struct P
{
    double x, y;
};

P a[120005];
int n, top, viz[120005];
int st[120005];

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

inline bool Cmp(P A, P B)
{
    if(A.y == B.y) return (A.x < B.x);
    return (A.y < B.y);
}

double F(int i, int j, P W)
{
    return (a[i].y - a[j].y) * W.x + (a[j].x - a[i].x) * W.y +
            (a[i].x * a[j].y - a[j].x * a[i].y);
}

void Hull()
{
    P R;
    int i;
    st[1] = 1;
    st[2] = 2;
    top = 2;
    viz[1] = viz[2] = 1;
    for(i = 3; i <= n; i++)
    {
        R = a[i];
        while(top >= 2 && F(st[top - 1], st[top], R) < 0)
        {
            viz[st[top]] = 0;
            top--;
        }
        st[++top] = i;
        viz[i] = 1;
    }

    viz[1] = 0;
    for(i = n; i >= 1; i--)
        if(viz[i] == 0)
    {
        R = a[i];
        while(top >= 2 && F(st[top - 1], st[top], R) < 0)
        {
            viz[st[top]] = 0;
            top--;
        }
        st[++top] = i;
        viz[i] = 1;
    }
}

void Afisare()
{
    top--;
    fout << top << "\n";
    fout << setprecision(12) << fixed;
    for(int i = 1; i <= top; i++)
        fout << a[st[i]].x << " " << a[st[i]].y << "\n";
    fout.close();
}

int main()
{
    Citire();
    sort(a + 1, a + n + 1, Cmp);
    Hull();
    Afisare();
    return 0;
}