Cod sursa(job #553244)

Utilizator BuRNB Radu BuRN Data 13 martie 2011 19:53:17
Problema Infasuratoare convexa Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

#define nmax 100001
int n, poz, st[nmax], vf, pp;

struct punct
{
	double x, y;
	double up;
}p[nmax];

bool cmp(punct a, punct b)
{ return a.up < b.up; }

int cmp2(punct A, punct B)
{
    return (double) (A.x-p[1].x)*(B.y-p[1].y) > (double) (B.x-p[1].x)*(A.y-p[1].y);
}

void push(int x)
{ st[++vf] = x; }

void pop()
{ vf--; }

void citire()
{
	freopen("infasuratoare.in","r",stdin); scanf("%d", &n);
	for(int i=1; i<=n; i++)
		scanf("%lf %lf", &p[i].x, &p[i].y);
}

void afisare()
{
	freopen("infasuratoare.out","w",stdout);
	printf("%d\n", vf);
	long i=0;
	while(vf)
	{
		printf("%f %f\n", p[st[vf]].x, p[st[vf]].y);
		pop();
	}
}

void caut_pmin()
{
	poz = 1;
	for(int i=1; i<=n; i++)
		if(p[i].y <= p[poz].y)
			if(p[i].y < p[poz].y || p[i].x < p[poz].x)
				poz = i;
}

double dist(int i)
{
	return cos( abs(p[poz].x-p[i].x) / sqrt( pow(p[poz].x-p[i].x,double(2))+pow(p[poz].y-p[i].y, double(2)) ) );
}

void unghi_p()
{
	for(int i=1; i<=n; i++)
		if(i==poz)
			p[i].up = 0;
		else
			p[i].up = dist(i);
}


int directie(int p1, int p2, int p3)
{
	return (p[p2].x-p[p1].x)*(p[p3].y-p[p1].y) > (p[p3].x-p[p1].x)*(p[p2].y-p[p1].y);
}

bool cmp3(punct a, punct b)
{
	return (a.x-p[poz].x)*(b.y-p[poz].y) > (b.x-p[poz].x)*(a.y-p[poz].y);
}

void solve()
{
	double o;
	caut_pmin();
	unghi_p();
	
	punct pc; pc = p[1]; p[1] = p[poz]; p[poz] = pc; poz=1;
	sort(p+2, p+1+n, cmp3);
	
	push(1); push(2);
	for(int i=3; i<=n; i++)
	{
		if( directie(st[vf-1], st[vf], i) )
			push(i);
		else
		{
			while( !directie(st[vf-1], st[vf], i)  && vf>=1)
				pop();
			 push(i);
		}
	}
}

int main()
{
	citire();
	solve();
	afisare();
	return 0;
}