Cod sursa(job #2753930)

Utilizator Laza_AndreiLazarescu Andrei Vlad Laza_Andrei Data 24 mai 2021 19:01:36
Problema Infasuratoare convexa Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const double eps=1e-10;
const int NMAX=10005;

struct POINT
{
  double x,y;
};

POINT p[NMAX],LL;
int h[NMAX];

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

int CCW(POINT P1,POINT P2,POINT P3)
{
    double C=cp(P1,P2,P3);

    if(fabs(C)<eps)
        return 0;
    if(C>=eps)
        return 1;
    return -1;
}

bool cmp(POINT a,POINT b)
{
    return CCW(LL, a, b) > 0;
}

int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);

    int n,i,top;
    scanf("%d",&n);
    scanf("%lf%lf",&p[0].x,&p[0].y);
    for(i=1;i<n;++i)
    {
        scanf("%lf%lf",&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);

    top=1;
    h[0]=0;
    h[1]=1;
    i=2;
    p[n]=p[0];

    while(i<=n)
    {
        if(CCW(p[h[top-1]],p[h[top]],p[i])>0){
            h[++top]=i;
            i++;
        }else{
            top--;
        }
    }

    printf("%d\n",top);
    for(i=0;i<top;++i){
        printf("%.6lf %.6lf\n",p[h[i]].x,p[h[i]].y);
    }
    return 0;
}