Cod sursa(job #2746855)

Utilizator pizzandreeaCiurescu Andreea pizzandreea Data 28 aprilie 2021 16:51:46
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <iostream>
#include <fstream> 
#include <unordered_map>
#include <vector>

using namespace std;

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

struct termeni{
    int a, b, c;
};

unordered_map <int, termeni> sume;

vector <int> v;  // valorile date la loto

void solve(int N, int S){

    // incercam toate combinatiile de 3 numere
    int suma;
    bool sol = 0;   //verificam daca am gasit o solutie

    for(int i = 0; i < N; i++)
        for(int j = i; j < N; j++)
            for(int k = j; k < N; k++){

                suma = v[i] + v[j] + v[k];

                if(S > suma){
                    
                    termeni t;
                    t.a = v[i];
                    t.b = v[j];
                    t.c = v[k];

                    sume.insert(pair<int, termeni> (suma, t));
                }
                if(S > suma && sume.find(S - suma) != sume.end()){

                    fout << v[i] << " " << v[j] << " " << v[k] << " ";
                    fout << sume[S - suma].a << " " << sume[S - suma].b << " " << sume[S - suma].c;
                    sol = 1;    

                    i = N;
                    j = N;
                    k = N;
                }
                
            }
    if( sol == 0){
        fout << -1;
    }
}

int main(){

    int N, S;
    int x;

    fin >> N >> S;

    for(int i = 0; i < N; i++){
        
        fin >> x;
        v.push_back(x);
    }
    solve(N, S);

    fin.close();
    fout.close();


    return 0;
}