Cod sursa(job #1166506)

Utilizator StefansebiStefan Sebastian Stefansebi Data 3 aprilie 2014 17:12:04
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include<fstream>
#include<algorithm>
#include<iomanip>
using namespace std;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
struct punct {
    double x; double y;
};
punct p[120001];
punct st[120001];
int n, i, xm, vf;

double prod(punct p0, punct p1, punct p2){
    return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
}

int cmp(punct p1, punct p2){
    return prod(p[1], p1, p2) < 0;
}


int main(){
    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> p[i].x >> p[i].y;
    xm = 1;
    for (i = 1; i <= n; i++)
        if (p[i].x < p[xm].x)
            xm = i;
    for (i = 1; i <= n; i++)
        if (p[i].x == p[xm].x and p[i].y < p[xm].y)
            xm = i;
    //fout << xm << " "; //cel mai mic punctulet
    punct aux = p[xm]; p[xm] = p[1]; p[1] = aux;
    sort(p + 2, p + n + 1, cmp);
    p[n + 1] = p[1];
   // for (i = 1; i <= n; i++)
    //    fout << p[i].x << " " << p[i].y << '\n';
    st[1] = p[1]; st[2] = p[2]; vf = 2;
    for (i = 3; i <= n; i++){
        while (vf >= 2 and prod(st[vf - 1], st[vf], p[i]) > 0)
            vf--;
        st[++vf] = p[i];
    }
    fout << vf << '\n';
    for (i = vf; i >= 1; i--)
        fout << fixed << setprecision(9) << st[i].x << " " << st[i].y << '\n';
}