Cod sursa(job #885405)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 21 februarie 2013 22:17:20
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <algorithm>
#define x first
#define y second
#define dd pair <double, double>
#define MAX_N 120005
using namespace std;
const char iname[] = "infasuratoare.in";
const char oname[] = "infasuratoare.out";
ifstream fin(iname);
ofstream fout(oname);
int n, head, i, pos;
dd v[MAX_N];
dd stack[MAX_N];
void read() 
{
    fin >> n;
    for (i = 1; i <= n; ++i)
        fin >> v[i].x >> v[i].y;
}
// GHIV IT TU MI BEIBI    AHAU AHAU 
inline double YOU_KNOW_ITS_COOL(const dd &A, const dd &B, const dd &C)
{
	double p = (-1) * A.x; double p2 = (-1) * A.y;
	double a = p + B.x; double b = p2 + B.y;
	double c = p + C.x; double d = p2 + C.y;
	return (a * d - (b * c));
}
struct cmp
{
	bool operator() (const dd &p1, const dd &p2) const
	{
		if (YOU_KNOW_ITS_COOL(v[1], p1, p2) < 0) return 1;
		return 0;
	};
};
void sort_points() 
{
    pos = 1;
    for (i = 2; i <= n; ++i)
        if (v[i] < v[pos])
            pos = i;
    swap(v[1], v[pos]);
    sort(v + 2, v + n + 1, cmp());
}
void convex_hull()
{
    sort_points();
    stack[1] = v[1];
    stack[2] = v[2];
    head = 2;
    for (i = 3; i <= n; ++i) 
	{
        while (head >= 2 && YOU_KNOW_ITS_COOL(stack[head - 1], stack[head], v[i]) > 0)
            --head;
        stack[++head] = v[i];
    }
}
void write() 
{
    fout << fixed;
    fout << head << "\n";
    for (i = head; i >= 1; --i)
        fout << setprecision(9) << stack[i].x << " " << stack[i].y << "\n";
}
int main() 
{
    read();
    convex_hull();
    write();
	return 0;
}