Cod sursa(job #2745040)

Utilizator TonioAntonio Falcescu Tonio Data 25 aprilie 2021 19:55:44
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

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

struct loto
{
    int suma;
    int x, y, z;
};
loto aux;
vector <loto> nr;
int n, s, x, v[101];
bool comp(loto a, loto b)
{
    return a.suma < b.suma;
}

int main()
{
    in >> n >> s;
    for(int i = 0; i < n; ++i)
        in >> v[i];
    for(int i = 0; i < n; ++i)
        for(int j = i; j < n; ++j)
            for(int k = j; k < n; ++k)
            {
                aux.suma = v[i] + v[j] + v[k];
                aux.x = v[i];
                aux.y = v[j];
                aux.z = v[k];
                nr.push_back(aux);
            }
    sort(nr.begin(), nr.end(), comp);
    int st, dr, ok = 0;
    st = 0;
    dr = nr.size() - 1;
    while(st <= dr)
    {
        if(nr[st].suma + nr[dr].suma > s)
            dr--;
        else if(nr[st].suma + nr[dr].suma < s)
            st++;
        else
        {
            out << nr[st].x << " " << nr[st].y << " " << nr[st].z << " " << nr[dr].x << " " << nr[dr].y << " " << nr[dr].z;
            ok = 1;
            break;
        }
    }
    if(!ok)
        out << -1;
    return 0;
}