Cod sursa(job #2742442)

Utilizator bananamandaoneTudor Cosmin Oanea bananamandaone Data 20 aprilie 2021 22:24:14
Problema Loto Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("loto.in");
ofstream fout("loto.out");

struct tuplu
{
    int x, y, z;
    tuplu() : x(0), y(0), z(0) {}
    tuplu(int X, int Y, int Z) : x(X), y(Y), z(Z) {}
};
unordered_map<int, tuplu>h;
vector<int>a;
int n, s;

void Citire()
{
    int i, x;
    fin >> n >> s;
    for(i = 1; i <= n; i++)
    {
        fin >> x;
        a.push_back(x);
    }
}

void Rezolvare()
{
    int i, j, k, suma, s1, s2, s3, s4, s5, s6;
    // toate sumele de cate 3 elemente
    for(i = 0; i < (int)a.size(); i++)
        for(j = 0; j < (int)a.size(); j++)
            for(k = 0; k < (int)a.size(); k++)
            {
                suma = a[i] + a[j] + a[k];
                h[suma] = tuplu(a[i], a[j], a[k]);
            }

    for(auto i : h)
    {
        // caut (s - suma din cele 3 elemente)
        // returneaza un iterator spre suma aia
        auto poz = h.find(s - i.first);

        // daca gasesc afisez solutia
        if(poz != h.end())
        {
            s1 = i.second.x;
            s2 = i.second.y;
            s3 = i.second.z;
            s4 = s - i.first - poz->second.y - poz->second.z;
            s5 = s - i.first - poz->second.x - poz->second.z;
            s6 = s - i.first - poz->second.x - poz->second.y;

            fout << s1 << " " << s2 << " " << s3 << " " << s4 << " " << s5 << " " << s6 << "\n";
            return;
        }
    }
    fout << -1;
}

int main()
{
    Citire();
    Rezolvare();

    fin.close();
    fout.close();
    return 0;
}