Cod sursa(job #202316)

Utilizator piroslPiros Lucian pirosl Data 7 august 2008 13:10:27
Problema Loto Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct nod 
{
	int s;
	int i1,i2,i3;
};

bool compare(nod n1, nod n2)
{
	return n1.s < n2.s;
}

int numere[101];

int main(void)
{
	int n, s;
	vector<nod> noduri;
	freopen("loto.in", "r", stdin);
	freopen("loto.out", "w", stdout);
	cin >> n >> s;
	for(int i = 0;i<n;++i)
		cin >> numere[i];
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<n;++j)
		{
			for(int k=0;k<n;++k)
			{
				nod tnod;
				tnod.i1 = numere[i];
				tnod.i2 = numere[j];
				tnod.i3 = numere[k];
				tnod.s = tnod.i1 + tnod.i2 + tnod.i3;
				noduri.push_back(tnod);
			}
		}
	}

	sort(noduri.begin(), noduri.end(), compare);
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<n;++j)
		{
			for(int k=0;k<n;++k)
			{
				int tmpS = s - (numere[i] + numere[j] + numere[k]);
				nod tmpN;
				tmpN.s = tmpS;
				vector<nod>::iterator tnod = lower_bound(noduri.begin(), noduri.end(), tmpN, compare);
				if(tnod!=noduri.end() && tnod->s == tmpN.s)
				{
					cout << numere[i] << " " << numere[j] << " " << numere[k] << " " << tnod->i1 << " " << tnod->i2 << " " << tnod->i3 << endl;
					return 0;
				}
			}
		}
	}
	cout << -1 << endl;
	return 0;
}