Cod sursa(job #538731)

Utilizator tudorsTudor Siminic tudors Data 21 februarie 2011 21:05:07
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <stdio.h>
#include <algorithm>
#define N 150001
using namespace std;
typedef struct {double x,y;} PUNCT;
PUNCT X[N];
PUNCT S[N],P[N];
long long s,p;
int n,i;

FILE *f,*g;

bool operator < (const PUNCT &a, const PUNCT &b)
{
	if (a.y<b.y)
		return 1;
	else if (a.y==b.y)
		if (a.x<b.x)
			return 1;
	return 0;
}

int stanga(PUNCT &a, PUNCT &b, PUNCT &c)
{
	float st;
	st=a.x*b.y+b.x*c.y+c.x*a.y-b.y*c.x-a.y*b.x-c.y*a.x;
	if (st>0)
		return 1;
	else return 0;
}

int main()
{
	f=fopen("infasuratoare.in","r");
	g=fopen("infasuratoare.out","w");
	fscanf(f,"%d",&n);
	for (i=1;i<=n;++i)
		fscanf(f,"%lf %lf",&X[i].x,&X[i].y);
	sort(X+1,X+n+1);
	s=2;
	S[1]=X[1];
	S[2]=X[2];
	i=3;
	while (i<=n)
	{
		if (stanga(S[s-1],S[s],X[i]))
		{
			S[++s]=X[i];
			++i;
		}
		else
		{
			s--;
			if (s<2)
			{
				S[++s]=X[i];
				++i;
			}
		}
	}
	P[1]=X[n];
	P[2]=X[n-1];
	p=2;
	i=n-2;
	while (i>=1)
	{
		if (stanga(P[p-1],P[p],X[i]))
		{
			P[++p]=X[i];
			--i;
		}
		else
		{
			--p;
			if (p<2)
			{
				P[++p]=X[i];
				--i;
			}
		}
	}
	fprintf(g,"%lld\n",s+p-2);
	for (i=1;i<=s;++i)
		fprintf(g,"%.6lf %.6lf\n",S[i].x,S[i].y);
	for (i=2;i<=p-1;++i)
		fprintf(g,"%.6lf %.6lf\n",P[i].x,P[i].y);
	fclose(f);
	fclose(g);
	return 0;
}