Cod sursa(job #865947)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 27 ianuarie 2013 12:41:46
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
int n, s, v[110], i, j, k, nr, t, ok;

struct sase{
	int x, y, z, s;
} a[1000010];

bool cmp(sase x, sase y){
	return x.s<y.s;
}

int cauta(int p, int u, int y){
	if(p>u)
		return 0;
	int m=(p+u)/2;
	if(a[m].s==y)
		return m;
	if(a[m].s>y)
		return cauta(p, m-1, y);
	return cauta(m+1, u, y);
}

int main(){
	f>>n>>s;
	for(i=1; i<=n; i++)
		f>>v[i];
	f.close();
	nr=0;
	for(i=1; i<=n; i++)
		for(j=i; j<=n; j++)
			for(k=j; k<=n; k++)
			{
				nr++;
				a[nr].x=v[i];
				a[nr].y=v[j];
				a[nr].z=v[k];
				a[nr].s=v[i]+v[j]+v[k];
			}
	sort(a+1, a+nr+1, cmp);
	for(i=1; i<=nr; i++)
	{
		if((t=cauta(i, nr, s-a[i].s))!=0)
		{
			g<<a[i].x<<' '<<a[i].y<<' '<<a[i].z<<' '<<a[t].x<<' '<<a[t].y<<' '<<a[t].z<<"\n";
			ok=1;
			break;
		}
	}
	if(ok==0)
		g<<"-1\n";
	g.close();
	return 0;
}