Cod sursa(job #3129850)

Utilizator AlezuuZugravu Alexandra-Daniela Alezuu Data 15 mai 2023 23:55:37
Problema Loto Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>

using namespace std;

vector<int> num_cas(int n, long long sum, vector<int>& num_loto)
{
    unordered_map<long long, pair<int, pair<int, int>>> suma;

    /// sumele pentru triplete
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            for (int k = j; k < n; k++) {
                long long total = num_loto[i] + num_loto[j] + num_loto[k];
                suma[total] = make_pair(i, make_pair(j, k));
            }
        }
    }

    ///vedem daca o tripleta a carei suma este scazuta din suma mare se potriveste cu cu o suma din unordered map
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            for (int k = j; k < n; k++) {
                long long total = num_loto[i] + num_loto[j] + num_loto[k];
                if (suma.find(sum - total) != suma.end()) {
                    auto p = suma[sum - total];
                    return {num_loto[i], num_loto[j], num_loto[k], num_loto[p.first], num_loto[p.second.first], num_loto[p.second.second]};
                }
            }
        }
    }

    return {};
}

int main() {
    ifstream f("loto.in");
    ofstream g("loto.out");

    int n;
    long long s;
    f>> n >> s;

    vector<int> num_loto(n);
    for (int i = 0; i < n; i++) {
        f>> num_loto[i];
    }

    vector<int> nc = num_cas(n, n, num_loto);

    if (nc.empty()) {
        g<< "-1";
    } else {
        for (int i = 0; i < 6; i++) {
            g<<nc[i] << " ";
        }
    }


    return 0;
}