Cod sursa(job #1824971)

Utilizator zhm248Mustatea Radu zhm248 Data 8 decembrie 2016 17:09:03
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
double eps = 1e-12;
struct punct
{
	double x, y;
};

double cp(punct a, punct b, punct c)
{
	return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

double ccw(punct a, punct b, punct c)
{
	long double pc = cp(a, b, c);
	if(pc >= eps)
		return 1;
	if(pc <= -eps)
		return -1;
	return 0;
}

bool cmp1(punct a, punct b)
{
	if(fabs(a.y - b.y) < eps)
		return a.x - b.x <= -eps;
	else
		return a.y - b.y <= -eps;
}

punct v[120001];
int h[120001];
bool cmp2(punct a, punct b)
{
	return ccw(v[1], a, b) > 0;
}

int main()
{
	freopen("infasuratoare.in", "r", stdin);
	freopen("infasuratoare.out", "w", stdout);
	int n, i, top = 2;
	scanf("%d", &n);
	for(i = 1; i <= n; ++i)
		{
			scanf("%lf%lf", &v[i].x, &v[i].y);
		}
	sort(v + 1, v + n + 1, cmp1);
	sort(v + 2, v + n + 1, cmp2);
	h[1] = 1;
	h[2] = 2;
	i = 3;
	v[n + 1] = v[1];
	while(i <= n + 1)
		{
			if(ccw(v[h[top - 1]], v[h[top]], v[i]) > 0)
				{
					h[++top] = i;
					i++;
				}
			else
				top--;
		}
	top--;
	printf("%d\n", top);
	for(i = 1; i <= top; ++i)
		{
			printf("%lf %lf\n", v[h[i]].x, v[h[i]].y);
		}
	return 0;
}