Pagini recente » Cod sursa (job #2134566) | Cod sursa (job #2587839) | Cod sursa (job #1793302) | Cod sursa (job #3243345) | Cod sursa (job #3155843)
#include <fstream>
#include <unordered_map>
using namespace std;
int a[105], S, n;
struct Triplet
{
int x, y, z;
};
unordered_map<int, Triplet> sume;
int main()
{
ifstream fin("loto.in");
ofstream fout("loto.out");
fin >> n >> S;
for(int i = 1; i <= n; i++)
{
fin >> a[i];
}
// Populez unordered_map cu toate sumele de triplete => O(n^3)
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
for(int k = 1; k <= n; k++)
{
sume[a[i] + a[j] + a[k]] = {a[i], a[j], a[k]};
}
}
}
// Caut solutia => O(n^3)
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
for(int k = 1; k <= n; k++)
{
int comp = S - (a[i] + a[j] + a[k]);
if(sume.count(comp))
{
Triplet tr = sume[comp];
fout << a[i] << " " << a[j] << " " << a[k] << " "
<< tr.x << " " << tr.y << " " << tr.z << "\n";
return 0;
}
}
}
}
fout << -1;
return 0;
}