Cod sursa(job #925627)

Utilizator smaraldaSmaranda Dinu smaralda Data 24 martie 2013 17:20:29
Problema Infasuratoare convexa Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include<stdio.h>
#include<algorithm>
#include<math.h>
#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)
{
    int 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(LL,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 || (p[i].y==LL.y && p[i].x<LL.x))
                {
                    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("%lf %lf\n",p[h[i]].x,p[h[i]].y);
    return 0;
}