Cod sursa(job #1716664)

Utilizator eragon0502Dumitrescu Dragos eragon0502 Data 13 iunie 2016 12:38:31
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <cstdio>
#include <algorithm>
#include <cmath>
#define eps 1.e-12
using namespace std;
struct POINT {double x,y;};
POINT p[120010];
POINT LL;
int h[120010];
int ccw(POINT P1,POINT P2,POINT P3)
{
    double c;
    c=(P2.x-P1.x)*(P3.y-P2.y)-(P2.y-P1.y)*(P3.x-P2.x);
    if(fabs(c)<eps)return 0;
    if(c>=eps)return 1;
    return -1;
}

bool cmp(POINT P1,POINT P2)
{
    return ccw(p[0],P1,P2)>0;
}

int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);
    int top,i,n;
    double t1,t2;
    scanf("%d",&n);
    scanf("%lf %lf",&t1,&t2);
    p[0].x=t1;
    p[0].y=t2;
    LL=p[0];
    for(i=1;i<n;++i)
        {
            scanf("%lf %lf",&t1,&t2);
            p[i].x=t1; p[i].y=t2;
            if(p[i].y-LL.y<-eps||(fabs(p[i].y-LL.y)<eps&&p[i].x-LL.x<-eps))
                {
                    LL=p[i];
                    p[i]=p[0];
                    p[0]=LL;
                }
        }
    sort(p+1,p+n,cmp);
    p[n]=p[0];
    h[0]=0;
    h[1]=1;
    top=1;
    i=2;
    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;
}