Cod sursa(job #3205107)

Utilizator gianiferSpita Alexandru-Mihai gianifer Data 18 februarie 2024 19:38:28
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>

#define N_MAX 1000005

using namespace std;

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

struct loto
{
    int nr1, nr2, nr3, suma;
} v[N_MAX];
int n, S, a[105];
bool cmp(loto a, loto b)
{
    return a.suma < b.suma;
}
int total = 0;
int main()
{
    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++)
            {
                v[++total] = {a[i], a[j], a[k], a[i] + a[j] + a[k]};
            }
    sort(v + 1, v + total + 1, cmp);
    for (int i = 1; i <= total; i++)
    {
        int st = 1, dr = total;
        int sum = S - v[i].suma;
        while (st <= dr)
        {
            int mij = (st + dr) / 2;
            if (v[mij].suma == sum)
            {
                fout << v[i].nr1 << " " << v[i].nr2 << " " << v[i].nr3 << " " << v[mij].nr1 << " " << v[mij].nr2 << " " << v[mij].nr3;
                return 0;
            }
            if (sum > v[mij].suma)
                st = mij + 1;
            if (sum < v[mij].suma)
                dr = mij - 1;
        }
    }
    fout << "-1";
}