Cod sursa(job #3131924)

Utilizator dra_soloSolomon Dragos dra_solo Data 21 mai 2023 21:31:45
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
#include <algorithm>
using namespace std;
struct SumOfThree
{
    long sum, a, b, c;
};
bool comp(SumOfThree s1, SumOfThree s2)
{
    return s1.sum < s2.sum;
}
long n, s, v[100], n_sums = 0, l_bound, u_bound, solved = 0;
SumOfThree sums[1000000];
int main()
{
    ifstream in("loto.in");
    ofstream out("loto.out");
    in >> n >> s;
    for (long i = 0; i < n; ++i)
        in >> v[i];
    for (long i = 0; i < n; ++i)
        for (long j = i; j < n; ++j)
            for (long k = j; k < n; ++k)
            {
                sums[n_sums].sum = v[i] + v[j] + v[k];
                sums[n_sums].a = v[i];
                sums[n_sums].b = v[j];
                sums[n_sums].c = v[k];
                ++n_sums;
            }
    sort(sums, sums + n_sums, comp);
    l_bound = 0;
    u_bound = n_sums - 1;
    while (l_bound <= u_bound)
    {
        if (sums[l_bound].sum + sums[u_bound].sum < s)
            ++l_bound;
        else if (sums[l_bound].sum + sums[u_bound].sum > s)
            --u_bound;
        else
        {
            solved = 1;
            out << sums[l_bound].a << ' ' << sums[l_bound].b << ' ' << sums[l_bound].c << ' '
                << sums[u_bound].a << ' ' << sums[u_bound].b << ' ' << sums[u_bound].c;
            break;
        }
    }
    if (!solved)
        out << "-1";
    in.close();
    out.close();
    return 0;
}