Cod sursa(job #780712)

Utilizator fhandreiAndrei Hareza fhandrei Data 21 august 2012 00:44:11
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
//Include
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

//Definitii
#define pb push_back

//Structuri
struct mystr
{
	int value;
	int first, second, third;
}newElement, searched;

//Clase
class compare
{
	public:
	bool operator() (mystr a, mystr b)
	{	return a.value < b.value;	}
};

//Variabile
ifstream in("loto.in");
ofstream out("loto.out");

int eNum, sum;
int numbers[101];

vector<mystr> sums;
vector<mystr>::iterator it1, it2, end;

//Main
int main()
{
	in >> eNum >> sum;
	for(int i=1 ; i<=eNum ; ++i)
		in >> numbers[i];
	
	sums.reserve(eNum*eNum*eNum);
	
	for(int i=1 ; i<=eNum ; ++i)
		for(int j=i ; j<=eNum ; ++j)
			for(int k=j ; k<=eNum ; ++k)
			{
				newElement.value = (newElement.first = numbers[i]) + (newElement.second = numbers[j]) + (newElement.third = numbers[k]);
				sums.pb(newElement);
			}
	
	sort(sums.begin(), sums.end(), compare());
	end = unique(sums.begin(), sums.end(), compare());
	
	for(it1=sums.begin() ; it1!=end ; ++it1)
	{
		searched.value = sum - it1->value;
		it2 = lower_bound(it1, end, searched, compare());
			if(it2->value == searched.value)
			{
				out << it1->first << ' ' << it1->second << ' ' << it1->third << ' ' << it2->first << ' ' << it2->second << ' ' << it2->third << '\n';
				in.close();
				out.close();
				return 0;
			}
	}
	
	
	out << -1 << '\n';
	in.close();
	out.close();
	return 0;
}