Cod sursa(job #2787424)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 23 octombrie 2021 11:47:51
Problema Loto Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>
using namespace std;
/**

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

struct trei
{
    short i, j, k;
    int suma; /// suma = a[i]+a[j]+a[k]
    bool operator<(const trei w) const
    {
        return suma < w.suma;
    }
};
int a[103], n, s;
trei t[200000];
int nt;

int CautBin(int x, int st, int dr)
{
    int mid;
    if (x < 0) return -1;
    while (st <= dr)
    {
        mid = (st + dr) / 2;
        if (t[mid].suma == x) return mid;
        if (t[mid].suma < x) st = mid + 1;
        else dr = mid - 1;
    }
    return -1;
}

int main()
{
    short i, j, k;
    int x, p;
    trei w;
    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++)
            {
                w.i = i; w.j = j; w.k = k;
                w.suma = a[i] + a[j] + a[k];
                t[nt++] = w;
            }
    sort(t, t + nt);
    for (i = 0; i < nt; i++)
    {
        x = t[i].suma;
        /// cauta binar daca in t[] exista elementul care are
        /// suma s - x > 0
        p = CautBin(s - x, i, nt - 1);
        if (p >= 0)
        {
            fout << t[i].i << " " << t[i].j << " " << t[i].k << " ";
            fout << t[p].i << " " << t[p].j << " " << t[p].k << "\n";
            fout.close();
            return 0;
        }
    }
    fout << "-1\n";
    fout.close();
    return 0;
}