Cod sursa(job #932997)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 29 martie 2013 14:46:52
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <iomanip>
#include <algorithm>
#define nmax 120100
using namespace std;

struct punct{double X,Y;}Punct[nmax];

punct Stack[nmax];
int N,Top;

inline int Det(const punct &A,const punct &B,const punct &C) {

    return A.X*B.Y + A.Y*C.X + B.X*C.Y - B.Y*C.X - A.X*C.Y - B.X*A.Y;

}
inline bool compare(const punct &A,const punct &B) {

    return Det(Punct[1],A,B) < 0;

}
void solve() {

    int i,P;

    P=1;
    for(i=2;i<=N;i++)
        if(Punct[i].X < Punct[P].X || (Punct[i].X == Punct[P].X && Punct[i].Y < Punct[P].Y) )
            P=i;

    swap(Punct[P],Punct[1]);
    sort(Punct+1,Punct+N+1,compare);

    Stack[++Top]=Punct[1];
    Stack[++Top]=Punct[2];

    for(i=3;i<=N;i++) {

        while( Top>=2 && Det(Stack[Top-1], Stack[Top], Punct[i]) > 0 )
            Top--;

        Stack[++Top]=Punct[i];

        }

}
void read() {

    ifstream in("infasuratoare.in");
    in>>N;

    for(int i=1;i<=N;i++)
        in>>Punct[i].X>>Punct[i].Y;

    in.close();

}
void write() {

    ofstream out("infasuratoare.out");

    out<<Top<<'\n';

    for(int i=Top;i>=1;i--)
        out<<fixed<<setprecision(12)<<Stack[i].X<<' '<<Stack[i].Y<<'\n';

    out.close();

}
int main() {

    read();
    solve();
    write();

    return 0;

}