Cod sursa(job #3155843)

Utilizator tonealexandruTone Alexandru tonealexandru Data 9 octombrie 2023 20:59:26
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#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;
}