Cod sursa(job #3219788)

Utilizator alex_0747Gheorghica Alexandru alex_0747 Data 1 aprilie 2024 11:23:05
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>
using namespace std;

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

struct Punct
{
    double x, y;
    bool operator< (Punct A) const
    {
        if (y == A.y) return x < A.x;
        return y < A.y;
    }
};

Punct a[125000];
int v[125000], st[125000], top, n;

double Plan(Punct A, Punct B, Punct C)
{
    double a, b, c;
    a = A.y - B.y;
    b = B.x - A.x;
    c = A.x * B.y - A.y * B.x;
    return a * C.x + b * C.y + c;
}

int main()
{
    int i;
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> a[i].x >> a[i].y;
    sort (a + 1, a + n + 1);
    st[1] = v[1] = v[2] = 1;
    st[top = 2] = 2;
    for (i = 3; i <= n; i++)
    {
        while (top > 1 &&
               Plan(a[st[top - 1]], a[st[top]], a[i]) <= 0)
        {
            v[st[top]] = 0;
            top--;
        }
        st[++top] = i;
        v[i] = 1;
    }
    v[1] = 0;
    for (i = n - 1; i >= 1; i--)
    {
        if (v[i]) continue;
        while (top > 1 &&
               Plan(a[st[top - 1]], a[st[top]], a[i]) <= 0)
            top--;
        st[++top] = i;
    }
    fout << top - 1 << "\n";
    fout << fixed;
    for (i = 1; i < top; i++)
        fout << setprecision(12) << a[st[i]].x << " " << a[st[i]].y << "\n";
    return 0;
}