Cod sursa(job #2832767)

Utilizator AswVwsACamburu Luca AswVwsA Data 14 ianuarie 2022 11:40:55
Problema Loto Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <bitset>
using namespace std;

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

struct sol
{
    int x, y, z;
};

vector <pair <int, sol> > fck;

bool cmp(pair <int, sol> a, pair <int, sol> b)
{
    return a.first < b.first;
}

void print(sol a)
{
    cout << a.x << " " << a.y << " " << a.z << " ";
}
set <int> bro;
set <int> :: iterator it1, it2, it3;
int main()
{
    int n, s, i;
    cin >> n >> s;
    for (i = 1; i <= n; i++)
        {
            int x;
            cin >> x;
            bro.insert(x);
        }
    for (it1 = bro.begin(); it1 != bro.end(); it1++)
        for (it2 = bro.begin(); it2 != bro.end(); it2++)
            for (it3 = bro.begin(); it3 != bro.end(); it3++)
            {
                sol a;
                a.x = *it1;
                a.y = *it2;
                a.z = *it3;
                fck.push_back(make_pair(*it1 + *it2 + *it3, a));
            }
    sort(fck.begin(), fck.end(), cmp);
    int p1, p2;
    p1 = -1;
    for (int i = 0; i < fck.size(); i++)
    {
        int val = s - fck[i].first;
        int st = 0, dr = fck.size() - 1, rez = -1;
        bool ok = 0;
        while (st <= dr)
        {
            int med = ((st + dr) >> 1);
            if (fck[med].first == val)
            {
                rez = med;
                ok = 1;
                break;
            }
            else if (fck[med].first > val)
                dr = med - 1;
            else
                st = med + 1;
        }
        if (ok)
        {
            p1 = i;
            p2 = rez;
            break;
        }
    }
    if (p1 == -1)
        cout << -1;
    else
    {
        print(fck[p1].second);
        print(fck[p2].second);
    }
}