Cod sursa(job #2402116)

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

using namespace std;
const double eps=1e-14;
const double INF=1e9;
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);
    scanf("%lf%lf",&a,&b);
    aux.x=a;
    aux.y=b;
    v.push_back(aux);
    for(i=2;i<=n;++i)
    {
        scanf("%lf%lf",&a,&b);
        aux.x=a;aux.y=b;
        v.push_back(aux);
        if(fabs(b-v[0].y)<eps)
        {
            if(a-v[0].x<-eps)
                swap(v[0],v[i-1]);
        }
        else
            if(b-v[0].y<-eps)
                swap(v[0],v[i-1]);
    }
    ll.x=v[0].x;ll.y=v[0].y;
    sort(v.begin()+1,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-1);
    for(i=1;i<=top-1;++i)
        printf("%lf %lf\n",v[st[i]].x,v[st[i]].y);
    return 0;
}