Cod sursa(job #3214839)

Utilizator CobzaruAntonioCobzaru Paul-Antonio CobzaruAntonio Data 14 martie 2024 14:58:15
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
ifstream cin ("infasuratoare.in");
ofstream cout ("infasuratoare.out");
struct punct
{
    long double x;
    long double y;
};
punct p0;
int delulu;
punct q[120005];
punct v[120005];
long double dist(punct a,punct b)
{
    long double rasp = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
    return rasp;
}
bool orientare(punct a,punct b,punct c)
{
    long double v = (b.y-a.y)*(c.x-a.x) - (c.y-a.y)*(b.x-a.x);
    if(v==0)
        return 0;
    if(v>0)
        return 1;
    return 2;
}
bool fcmp(punct a,punct b)
{
    int o = orientare(p0,a,b);
    if(o==0)
    {
        if(dist(p0,a)>=dist(p0,b))
            return false;
        return true;
    }
    return o==2;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
        cin >> v[i].x >> v[i].y;
    delulu = 1;
    p0 = v[1];
    for(int i=2;i<=n;i++)
    {
        if(v[i].y<v[delulu].y)
            delulu = i;
        else if (v[i].y==v[delulu].y)
        {
            if(v[i].x<v[delulu].x)
                delulu = i;
        }
    }
    swap(v[1],v[delulu]);
    p0 = v[1];
    sort(v+2,v+n+1,fcmp);
    int vf = 2;
    q[1] = v[1];
    q[2] = v[2];
    for(int i=3;i<=n;i++)
    {
        while(vf>1 && orientare(q[vf-1],q[vf],v[i])!=2)
            vf--;
        q[++vf] = v[i];
    }
    cout << vf << '\n';
    for(int i=1;i<=n;i++)
        cout << fixed <<setprecision(15) << q[i].x << ' ' << q[i].y << '\n';
    return 0;
}