Cod sursa(job #2753918)

Utilizator Gabriel_DascalescuGabriel Dascalescu Gabriel_Dascalescu Data 24 mai 2021 18:47:13
Problema Infasuratoare convexa Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;

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

const int nmax = 10000;

const double eps = 1.e-18;

struct POINT
{
    double x, y;
};

POINT p[nmax+5];

POINT LL;

double cp(const POINT P1, const POINT P2, const POINT P3)
{
    return (P2.x-P1.x)*(P3.y-P2.y)-(P2.y-P1.y)*(P3.x-P2.x);
}

int ccw(const POINT P1, const POINT P2, const POINT P3)
{
    /*1 trigonometric 0 coliniar -1 ceasornic*/
    if(fabs(cp(P1,P2,P3))<eps)
        return 0;
    if(cp(P1,P2,P3)>=eps)
        return 1;
    return -1;
}

bool cmp(POINT P1, POINT P2)
{
    return ccw(LL, P1, P2)>0;
}

int n, top, h[nmax+5], i;

int main()
{
    in>>n;
    in>>p[0].x>>p[0].y;
    for(i=1; i<n; i++)
    {
        in>>p[i].x>>p[i].y;
        if(p[i].y - p[0].y<=-eps || (fabs(p[i].y-p[0].y)<eps && p[i].x - p[0].x <= -eps))
           {
               swap(p[0],p[i]);
           }
    }
    LL = p[0];
    sort(p+1, p+n, cmp);
    h[0]=0;
    h[1]=1;
    top=1;
    p[n]=p[0];
    i=2;
    while(i<=n)
    {
        if(ccw(p[h[top-1]],p[h[top]],p[i])>0)
        {
            h[++top]=i;
            i++;
        }
        else
        {
            top--;
        }
    }
    out<<top;
    out<<"\n";
    for(i=0; i<top; i++)
    {
        out<<fixed<<setprecision(6)<<p[h[i]].x<<" "<<fixed<<setprecision(6)<<p[h[i]].y<<"\n";
    }
    return 0;
}