Cod sursa(job #2609216)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 2 mai 2020 12:33:33
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda greseli_destepte Marime 1.24 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

struct sumstruct
{
    int s;
    int a, b, c;
} sum[1000001];

bool comp(sumstruct a, sumstruct b)
{
    return a.s < b.s;
}

int a[101], lg;

int cb(int x)
{
    if (x < 0)
        return 0;
    int st = 1, dr = lg, mij;
    while (st<=dr)
    {
        mij = (st+dr)>>1;
        if (sum[mij].s == x)
            return mij;
        if (sum[mij].s < x)
            st = mij + 1;
        else
            dr = mij - 1;
    }
    return 0;
}

int main()
{
    int i, j, k, n, s, p;
    fin >> n >> s;
    for (i = 1; i<=n; i++)
        fin >> a[i];
    for (i = 1; i<=n; i++)
        for (j = i; j<=n; j++)
            for (k = j; k<=n; k++)
                sum[++lg] = {a[i]+a[j]+a[k], a[i], a[j], a[k]};
    sort(sum+1, sum+lg+1, comp);
    for (i = 1; i<=n; i++)
        for (j = i; j<=n; j++)
            for (k = j; k<=n; k++)
            {
                p = cb(s - a[i] - a[j] - a[k]);
                if (p != 0)
                {
                    fout << a[i] << ' ' << a[j] << ' ' << a[k] << ' ' << sum[p].a << ' ' << sum[p].b << ' ' << sum[p].c;
                    return 0;
                }
            }
    fout << -1;
    return 0;
}