Cod sursa(job #109932)

Utilizator ada_sAda-Mihaela Solcan ada_s Data 25 noiembrie 2007 12:57:42
Problema Aliens Scor 10
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasa a 10-a Marime 3.29 kb
#include <fstream>
struct f{long x, y;};
struct fr{int x[3], y[3];};

std::ifstream f1("aliens.in");
std::ofstream f2("aliens.out");

void mergesort(struct f sir[55], int jos, int sus);
void merge(struct f sir[55], int jos, int mij, int sus);
void transf(long a, int b[3]);

struct fr frac[55];
int xtot[3], ytot[3], total[3];

int main()
{
	int n, poz=0, i, j;
	struct f temp[55];
	bool corect;
	long nr=0, t;
	f1>>n;
	for (i=0; i<n; i++)
	{
		f1>>temp[i].x;
		f1>>temp[i].y;
		if ((temp[i].x/temp[i].y)>0)
			poz++;
	}//for i
	mergesort(temp, 0, n-1);
	for (i=0; i<n; i++)
	{
    transf(temp[i].x, frac[i].x);
    transf(temp[i].y, frac[i].y);		
		for (j=0; j<3; j++)
		{
			if (frac[i].x[j]>frac[i].y[j])
			{
				frac[i].x[j]-=frac[i].y[j];
				frac[i].y[j]=0;
			}//if
			else
			{
				frac[i].y[j]-=frac[i].x[j];
				frac[i].x[j]=0;
			}//if			
			total[j]+=frac[i].x[j];
		}//for j
	}//for i
	for (i=0; i<poz; i++)
	{
		corect=1;
		if ((ytot[0]+frac[i].y[0])>total[0])
			corect=0;
		if ((ytot[1]+frac[i].y[1])>total[1])
			corect=0;
		if ((ytot[2]+frac[i].y[2])>total[2])
			corect=0;
		if (corect)
		{
			xtot[0]+=frac[i].x[0];
			xtot[1]+=frac[i].x[1];
			xtot[2]+=frac[i].x[2];			
			ytot[0]+=frac[i].y[0];
			ytot[1]+=frac[i].y[1];
			ytot[2]+=frac[i].y[2];
			for (j=0; j<3; j++)
			{
				if (xtot[j]>ytot[j])
				{
					xtot[j]-=ytot[j];
					ytot[j]=0;
				}//if
				else
				{
					ytot[j]-=xtot[j];
					xtot[j]=0;
				}//if			
			}//for j
		}//if
		else
		{
			total[0]-=frac[i].x[0];
			total[1]-=frac[i].x[1];
			total[2]-=frac[i].x[2];
		}//else
	}//for i
	while (!((ytot[0]+ytot[1]+ytot[2])==0)&&(i<n))
	{
		corect=1;
		if ((ytot[0]+frac[i].y[0])>total[0])
			corect=0;
		if ((ytot[1]+frac[i].y[1])>total[1])
			corect=0;
		if ((ytot[2]+frac[i].y[2])>total[2])
			corect=0;
		if (corect)
		{
			xtot[0]+=frac[i].x[0];
			xtot[1]+=frac[i].x[1];
			xtot[2]+=frac[i].x[2];			
			ytot[0]+=frac[i].y[0];
			ytot[1]+=frac[i].y[1];
			ytot[2]+=frac[i].y[2];
			for (j=0; j<3; j++)
			{
				if (xtot[j]>ytot[j])
				{
					xtot[j]-=ytot[j];
					ytot[j]=0;
				}//if
				else
				{
					ytot[j]-=xtot[j];
					xtot[j]=0;
				}//if			
			}//for j
		}//if
		else
		{
			total[0]-=frac[i].x[0];
			total[1]-=frac[i].x[1];
			total[2]-=frac[i].x[2];
		}//else		
		i++;
	}//while
	t=1;
	for (i=0; i<xtot[0]; i++)
		t*=2;
	nr=t;
	t=1;
	for (i=0; i<xtot[1]; i++)
		t*=3;
	nr*=t;
	t=1;
	for (i=0; i<xtot[2]; i++)
		t*=5;
	nr*=t;	
	f2<<nr;
	f1.close();
	f2.close();
	return 0;
}//main

void mergesort(struct f sir[55], int jos, int sus)
{
	if (jos<sus)
	{
		int mij=(jos+sus)/2;
		mergesort(sir, jos, mij);
		mergesort(sir, mij+1, sus);
		merge(sir, jos, mij, sus);
	}//if
}//mergesort

void merge(struct f sir[55], int jos, int mij, int sus)
{
	int i1=jos, i2=mij+1, ib=jos, i;
	struct f b[55];
	while ((i1<=mij)&&(i2<=sus))
	{
		if ((((float)(sir[i1].x))/((float)(sir[i1].y))) > (((float)(sir[i2].x))/((float)(sir[i2].y))))
		{
			b[ib]=sir[i1];
			i1++;
		}//if
		else
		{
			b[ib]=sir[i2];
			i2++;
		}//else
		ib++;
	}//while
	for (i=i1; i<=mij; i++)
		b[ib+i-i1]=sir[i];
	for (i=i2; i<=sus; i++)
		b[ib+i-i2]=sir[i];
	for (i=jos; i<=sus; i++)
		sir[i]=b[i];
}//merge

void transf(long a, int b[3])
{
  while ((a%2)==0)
	{
		b[0]++;
		a/=2;
	}//while
  while ((a%3)==0)
	{
		b[1]++;
		a/=3;
	}//while
  while ((a%5)==0)
	{
		b[2]++;
		a/=5;
	}//while	
}//transf