Cod sursa(job #3154227)

Utilizator SSKMFSS KMF SSKMF Data 3 octombrie 2023 20:14:29
Problema Loto Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream cin ("loto.in");
ofstream cout ("loto.out");

pair < pair <int , int> , pair <int , int> > optiuni[1000001];

int main ()
{
    int lungime , suma;
    cin >> lungime >> suma;

    int sir[101] , total = 0;
    for (int indice_1 = 1 ; indice_1 <= lungime ; indice_1++)
    {
        cin >> sir[indice_1];

        for (int indice_2 = 1 ; indice_2 <= indice_1 ; indice_2++)
            for (int indice_3 = 1 ; indice_3 <= indice_2 ; indice_3++)
                optiuni[++total] = make_pair(make_pair(sir[indice_1] + sir[indice_2] + sir[indice_3] , sir[indice_1]) , make_pair(sir[indice_2] , sir[indice_3]));
    }

    sort(optiuni + 1 , optiuni + total + 1);

    for (int stanga = 1 , dreapta = total ; stanga <= dreapta ; dreapta--)
    {
        while (stanga < dreapta && optiuni[stanga].first.first < suma - optiuni[dreapta].first.first)
            stanga++;

        if (optiuni[stanga].first.first + optiuni[dreapta].first.first == suma)
        {
            int termeni[7] = {0};
            for (int indice = stanga ; indice != -1 ; indice = (indice == stanga ? dreapta : -1))
            {
                termeni[++termeni[0]] = optiuni[indice].first.second;
                termeni[++termeni[0]] = optiuni[indice].second.first;
                termeni[++termeni[0]] = optiuni[indice].second.second;
            }

            sort(termeni + 1 , termeni + 7);

            for (int indice = 1 ; indice <= 6 ; indice++)
                cout << termeni[indice] << ' ';

            cout.close(); cin.close();
            return 0;
        }
    }

    cout << "-1";
    cout.close(); cin.close();
    return 0;
}