Cod sursa(job #2911303)

Utilizator Mihai_999Diaconeasa Mihai Mihai_999 Data 28 iunie 2022 14:53:14
Problema Loto Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define nl '\n'

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

int n, s, a[110], ksp;
struct ind
{
    int ind1, ind2, ind3;
};
unordered_map<int, ind> x;
pair<int,ind> sp[1001000];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    fin >> n >> s;
    for (int i = 1; i <= n; i++)
        fin >> a[i];
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++)
            for (int k = j; k <= n; k++)
            {
                int sum = a[i]+a[j]+a[k];
                if (sum <= s && x.find(sum) == x.end())
                {
                    ind y;
                    y.ind1 = a[i];
                    y.ind2 = a[j];
                    y.ind3 = a[k];
                    x[sum] = y;
                    sp[++ksp].first = sum;
                    sp[ksp].second = y;
                }
            }
    for (int i = 1; i <= ksp; i++)
    {
        if (x.find(s-sp[i].first) != x.end())
        {
            ind f = sp[i].second, g = x[s-sp[i].first];
            fout << f.ind1 << ' ' << f.ind2 << ' ' << f.ind3 << ' ' << g.ind1 << ' ' << g.ind2 << ' ' << g.ind3;
            return 0;
        }
    }
    fout << -1;
    return 0;
}