Cod sursa(job #563998)

Utilizator scipianusFMI Ciprian Olariu scipianus Data 26 martie 2011 15:56:09
Problema Loto Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include<fstream>
using namespace std;
int a[105],n,s,contor;
struct Suma{int s,x1,x2,x3;};
Suma sum[1000005];

inline bool Ordonare(const Suma A,const Suma B)
{
	return A.s<B.s;
}

int CautareBinara(int x)
{
	int st,dr,m;
	st=0;
	dr=contor-1;
	while(st<=dr)
	{
		m=((st+dr)>>1);
		if(sum[m].s==x)
			return m;
		else
			if(sum[m].s>x)
				dr=m-1;
			else
				st=m+1;
	}
	return -1;
}

int main()
{
	int i,j,k;
	int st,dr,m,x;
	ifstream fin("loto.in");
	fin>>n>>s;
	for(i=0;i<n;i++)
		fin>>a[i];
	fin.close();
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			for(k=0;k<n;k++)
			{
				sum[contor].s=a[i]+a[j]+a[k];
				sum[contor].x1=a[i];
				sum[contor].x2=a[j];
				sum[contor].x3=a[k];
				contor++;
			}
	sort(sum,sum+contor,Ordonare);
	ofstream fout("loto.out");
	for(i=0;i<contor;i++)
	{
		st=0;
		dr=contor-1;
		x=s-sum[i].s;
		while(st<=dr)
		{
			m=((st+dr)>>1);
			if(sum[m].s==x)
			{
				fout<<sum[i].x1<<' '<<sum[i].x2<<' '<<sum[i].x3<<' ';
				fout<<sum[m].x1<<' '<<sum[m].x2<<' '<<sum[m].x3<<"\n";
				fout.close();
				return 0;
			}
			else
				if(sum[m].s>x)
					dr=m-1;
				else
					st=m+1;
		}
	}
	fout<<"-1"<<"\n";
	return 0;
}