Cod sursa(job #2402055)

Utilizator Alex_AeleneiAlex Aelenei Ioan Alex_Aelenei Data 10 aprilie 2019 12:15:23
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;
const double eps=1e-14;
struct punct
{
    double x,y;
};
punct ll;
double cp(punct a,punct b,punct c)
{
    return (b.x-a.x)*(c.y-b.y)-(b.y-a.y)*(c.x-b.x);
}
int ccw(punct a,punct b,punct c)
{
    double cpp=cp(a,b,c);
    if(fabs(cpp)<eps)return 0;
    if(cpp>eps)return 1;
    return -1;
}
bool cmp(punct a,punct b)
{
    int ccww=ccw(ll,a,b);
    return ccww==1;
}
vector <punct> v;
int st[120005];
int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);
    int n,i,top;
    double a,b;
    punct aux;
    scanf("%d",&n);
    ll.x=0;ll.y=0;
    for(i=1;i<=n;++i)
    {
        scanf("%lf%lf",&a,&b);
        aux.x=a;aux.y=b;
        if(fabs(a-ll.x)<eps)
        {
            if(b>ll.x)
                ll.x=a,ll.y=b;
        }
        else
            if(a<ll.x)
                ll.x=a;ll.y=b;
        v.push_back(aux);
    }
    sort(v.begin(),v.end(),cmp);
    v.push_back(ll);
    top=1;st[top]=0;i=2;
    st[++top]=1;
    while(i<v.size())
    {
        if(ccw(v[st[top-1]],v[st[top]],v[i])>0)
        {
            st[++top]=i;
            ++i;
        }
        else
            --top;
    }

    printf("%d\n",top);
    for(i=2;i<=top+1;++i)
        printf("%lf %lf\n",v[st[i]].x,v[st[i]].y);
    return 0;
}