Cod sursa(job #1603684)

Utilizator negulescuirinaIrina N negulescuirina Data 17 februarie 2016 18:38:18
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <fstream>
#include <cmath>
#include<iomanip>
#include <algorithm>
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
const double eps=1.e-14;
struct POINT
{
    double x,y;
};
POINT P[1005],LL;
double dist(POINT P1, POINT P2)
{
    return (P1.x-P2.x)*(P1.x-P2.x)+(P1.y-P2.y)*(P1.y-P2.y);
}
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 ccp;
    ccp=cp(P1,P2,P3);
    if(fabs(ccp)<eps)
        return 0;
    if(ccp>=eps)
        return 1;
    else return -1;

}
bool cmp(POINT P1, POINT P2)
{
    double d1,d2;
    int cccw=ccw(LL,P1,P2);
    d1=dist(LL,P1);
    d2=dist(LL,P2);
    if(cccw==0)
    {
        if(d1<d2)
            return 1;
        else return 0;
    }
    else if(cccw==1)
        return 1;
    else
        return 0;
}

int main()
{
    int n,top=1,st[1000],i;
    f>>n;
    f>>P[1].x>>P[1].y;
    LL.x=P[1].x;
    LL.y=P[1].y;
    for(i=2; i<=n; i++)
    {
        f>>P[i].x>>P[i].y;
        if(P[i].y-P[1].y<=-eps)
            swap(P[i],P[1]);
        else if(fabs(P[i].y-P[1].y)<eps and (P[i].x-P[1].x<=eps) )
            swap(P[i],P[1]);
    }
    LL=P[1];
    sort(P+2,P+n+1,cmp);
    P[n+1]=P[1];
    top=1;
    st[top]=1;
    top++;
    st[top]=2;
    i=3;
    while(i<=n+1)
    {
        if(ccw(P[st[top-1]],P[st[top]],P[i])>0)
        {
            st[++top]=i;
            i++;
        }
        else top--;
    }
    top--;
    g<<top<<'\n';

    ///setprecision(6);
    for(int k=1; k<=top; k++)
        {
            g<<fixed<<showpoint;
            g<<setprecision(6);
            g<<P[st[k]].x<<" "<<P[st[k]].y<<'\n';
        }


}