Cod sursa(job #2778230)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 30 septembrie 2021 18:38:21
Problema Loto Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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

struct comb
{
    int s,t1,t2,t3;
};

int n,s,a[105];
vector<comb>v;

int cb(int p)
{
    int caut = s - v[p].s;
    int st = 0,dr = v.size() - 1;
    while (st <= dr)
    {

        int mij = (st + dr) / 2;
        if (v[mij].s == caut)
            return mij;
        else if (v[mij].s > caut)
            dr = mij - 1;
        else
            st = mij + 1;
    }
    if (v[st].s == caut)
        return st;
    else if (v[dr].s == caut)
        return dr;
    else
        return -1;
}

int main()
{
    in >> n >> s;
    for (int i = 1; i <= n; i++)
        in >> a[i];
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j <= n; j++)
        {
            for (int q = j; q <= n; q++)
            {
                comb x;
                x.s = a[i] + a[j] + a[q];
                x.t1 = i;
                x.t2 = j;
                x.t3 = q;
                v.push_back(x);
            }
        }
    }
    for (int i = 0; i < v.size(); i++)
    {
        int x = cb(i);
        if (x != -1)
        {
            out << v[i].t1 << " " << v[i].t2 << " " << v[i].t3 << " " << v[x].t1 << " " << v[x].t2 << " " << v[x].t3;
            return 0;
        }
    }
    out << -1;
    return 0;
}