Cod sursa(job #1283833)

Utilizator toniobFMI - Barbalau Antonio toniob Data 6 decembrie 2014 01:47:54
Problema Loto Scor 80
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.21 kb
#include <fstream>
#include <unordered_map>
#include <vector>
using namespace std;

ifstream in("loto.in");
ofstream out("loto.out");
int n, s, v[10000];
unordered_map<int, vector<int> > h;

void read(){
	in >> n >> s;
	for (int i = 1; i <= n; in >> v[i++]);
}

void calc_sums(){
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= n; ++j){
			for (int k = 1; k <= n; ++k){
				h[ v[i]+v[j]+v[k] ] = { v[i], v[j], v[k] };
			}
		}
	}
}

void print_sol(){
	for (unordered_map<int, vector<int> >::iterator it = h.begin(), res; it != h.end(); ++it){
		res = h.find(s - it->first);
		if (res != h.end()) {
			vector<int> init = it->second, resv = res->second;
			out << init[0] << ' ' << init[1] << ' ' << init[2] << ' ';
			out << resv[0] << ' ' << resv[1] << ' ' << resv[2];
			return;
		}
	}
	out << "-1";
}

void print_sol2(){
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= n; ++j){
			for (int k = 1; k <= n; ++k){
				if (h.count(s - v[i]-v[j]-v[k]) > 0){
					vector<int> resv = h[s - v[i]-v[j]-v[k]];
					out << v[i] << ' ' << v[j] << ' ' << v[k] << ' ';
					out << resv[0] << ' ' << resv[1] << ' ' << resv[2];
					return;
				}
			}
		}
	}
	out << "-1";
}

int main(){
	read();

	calc_sums();

	print_sol2();

	return 0;
}