Cod sursa(job #2309493)

Utilizator florin_salamFlorin Salam florin_salam Data 29 decembrie 2018 02:56:20
Problema Loto Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

const int NMAX = 110;

struct val
{
	int cost, x, y, z;
	val()
	{
		this->cost = this->x = this->y = this->z = 0;
	}
	val(const int &cost, const int &x, const int &y, const int &z)
	{
		this->cost = cost;
		this->x = x;
		this->y = y;
		this->z = z;
	}
	inline bool operator<(const val &other) const
	{
		return this->cost < other.cost;
	}
};

vector <val> a;
int n, s, v[NMAX];

int main()
{
	ifstream fin("loto.in");
	ofstream fout("loto.out");
	fin >> n >> s;
	for (int i = 1;i <= n;++i)
		fin >> v[i];
	for (int i = 1;i <= n;++i)
		for (int j = i;j <= n;++j)
			for (int k = j;k <= n;++k)
			{
				int aux = v[i] + v[j] + v[k];
				a.push_back(val(aux, v[i], v[j], v[k]));
			}
	sort(a.begin(), a.end());
	int st = 0, dr = a.size() - 1;
	while (st < dr)
	{
		if (a[st].cost + a[dr].cost < s)
			++st;
		else if (a[st].cost + a[dr].cost > s)
			--dr;
		else
		{
			fout << a[st].x << " " << a[st].y << " " << a[st].z << " " << a[dr].x << " " << a[dr].y << " " << a[dr].z << "\n";
			return 0;
		}
	}
	fout << -1 << "\n";
	fin.close();
	fout.close();
	return 0;
}