Cod sursa(job #1006096)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 6 octombrie 2013 13:24:37
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
int n, i, j, st[120010], ind, k;

struct punct{
    double x, y;
} v[120010], p;

double afla(punct a, punct b, punct c){
    int t = (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
	return t;
}

bool cmp(punct a, punct b){
    double p1=b.x*a.y;
    double p2=a.x*b.y;
    if(p1<p2)
        return 1;
    return 0;
}

int main(){
    f>>n;
    p.x=1000000010;
    p.y=1000000010;
	
    for(i=1; i<=n; i++)
    {
        f>>v[i].x>>v[i].y;
        if(v[i].y<p.y || (v[i].y==p.y && v[i].x<p.x))
            p=v[i], ind=i;
    }
    v[ind]=v[1];
    v[1]=p;
    for(i=1; i<=n; i++)
        v[i].x-=p.x, v[i].y-=p.y;
	
    sort(v+2, v+n+1, cmp);
    
	st[1]=1;
    st[2]=2;
    k=2;
    
int t;

	for(i=3; i<=n; i++)
    {
        while(k>=2 && (t = afla(v[st[k-1] ], v[ st[k] ], v[i])<0))
            k--;
        st[++k]=i;
    }
    
	
	g<<k<<"\n"<<fixed;
    for(i=1; i<=k; i++)
        g<<setprecision(6)<<v[ st[i] ].x+p.x<<' '<<v[ st[i] ].y+p.y<<"\n";
    return 0;
}