Cod sursa(job #1449533)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 9 iunie 2015 21:50:46
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <vector>
#include <array>
#include <fstream>
#include <algorithm>
#include <numeric>
using namespace std;

using ll = long long;

ll suma(const array<ll, 3> x){
	return accumulate(begin(x), end(x), 0); }

bool operator<(const array<ll, 3>& lhs, const array<ll, 3>& rhs){
	return suma(lhs) < suma(rhs); }

int main(){
	ifstream f("loto.in");
	ofstream g("loto.out");
	int n, s;
	f >> n >> s;
	vector<array<ll, 3> > v;
	v.reserve(n*n*n);
	{
		vector<ll> nr(n, 0ll);
		for(auto& x : nr){
			f >> x; }
		for(auto a = begin(nr), en = end(nr); a != en; ++a){
			for(auto b = a; b != en; ++b){
				for(auto c = b; c != en; ++c){
					v.push_back(array<ll, 3>({*a, *b, *c})); } } } }
	sort(begin(v), end(v));
	auto stanga = begin(v), dreapta = end(v)-1;
	ll tmp;
	while(stanga <= dreapta){
		tmp = suma(*stanga) + suma(*dreapta);
		if(tmp < s){
			++stanga; }
		else if(tmp == s){
			for(const auto x : *stanga){
				g << x << ' '; }
			for(const auto x : *dreapta){
				g << x << ' '; }
			return 0; }
		else{
			--dreapta; } }
	//implicit n-am iesit cu returnul de mai sus
	g << "-1";
	return 0; }