Cod sursa(job #116858)

Utilizator hadesgamesTache Alexandru hadesgames Data 19 decembrie 2007 18:54:28
Problema Rays Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
struct ceva
{
	double i,s;
};
ceva a[200000],b[200000];
 int compare( const void* a, const void* b ) {
   ceva *aa = (ceva*) a;
   ceva *bb = (ceva*) b;
   if( aa->i < bb->i ) return -1;
   else if(aa->i == bb->i ) return 0;
   else return 1;
 }      
int main()
{
	FILE *in,*out;
	int n,nr=0,nr1=-1,nr2=-1,i,x,y1,y2,aux;
	double t,y;
	in=fopen("rays.in","r");
	out=fopen("rays.out","w");
	fscanf(in,"%d",&n);
	for (i=1;i<=n;i++)
	{
		fscanf(in,"%d%d%d",&x,&y1,&y2);
		if (y1>y2)
		{
			aux=y1;
			y1=y2;
			y2=aux;
		}
		if (x>0)
		{
			nr1++;
			if (y1==0)
				a[nr1].i=90;
			else
				if (y1>0)
					a[nr1].i=90+atan((double)y1/(double)x)*180/M_PI;
				else
					a[nr1].i=90-atan((double)abs(y1)/(double)x)*180/M_PI; 
			if (y2==0)
				a[nr1].s=90;
			else
				if (y2>0)
					a[nr1].s=90+atan((double)y2/(double)x)*180/M_PI;
				else
					a[nr1].s=90-atan((double)abs(y2)/(double)x)*180/M_PI;
		}
		else
		{
			nr2++;
			if (y1==0)
				b[nr2].i=90;
			else
				if (y1>0)
					b[nr2].i=90+atan((double)y1/(double)abs(x))*180/M_PI;
				else
					b[nr2].i=90-atan((double)abs(y1)/(double)abs(x))*180/M_PI; 
			if (y2==0)
				b[nr2].s=90;
			else
				if (y2>0)
					b[nr2].s=90+atan((double)y2/(double)abs(x))*180/M_PI;
				else
					b[nr2].s=90-atan((double)abs(y2)/(double)abs(x))*180/M_PI;
		}
	}
	qsort(a,nr1+1,sizeof(a[0]),compare);
	qsort(b,nr2+1,sizeof(b[0]),compare);
	nr=0;
	t=a[0].i;
	y=a[0].s;
	for (i=1;i<=nr1;i++)
	{
		if (a[i].i-t>=0.0001&&a[i].i-y<=0.0001)
		{
			t=a[i].i;
			if (a[i].s-y<0.0001)
				y=a[i].s;
		}
		else
		{
			nr++;
			t=a[i].i;
			y=a[i].s;
		}
	}
	if (nr1!=-1)
		nr++;
	t=b[0].i;
	y=b[0].s;
	for (i=1;i<=nr2;i++)
	{
		if (b[i].i-t>=0.0001&&b[i].i-y<=0.0001)
		{
			t=b[i].i;
			if (b[i].s-y<0.0001)
				y=b[i].s;
		}
		else
		{
			nr++;
			t=b[i].i;
			y=b[i].s;
		}
	}
	if (nr2!=-1)	
		nr++;
	fprintf(out,"%d\n",nr);
	fclose(in);
	fclose(out);
}