Cod sursa(job #1826030)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 9 decembrie 2016 23:23:18
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
# include <vector>
# include <fstream>
# include <algorithm>

# define MAX_N 100
# define infinit 100000001

using namespace std;

struct pereche
{
    int a, b, c, s;

    pereche( int a, int b, int c )
    {
        this->a = a;
        this->b = b;
        this->c = c;
        this->s = a + b + c;
    }
};

int v[MAX_N];
vector<pereche> l;
vector<pereche*> p;

bool cmp( const pereche * a, const pereche * b )
{
    return a->s < b->s;
}

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

    int n, s, i, j, k;

    fin >> n >> s;

    for ( i = 0; i < n; i ++ )
        fin >> v[i];

    for ( i = 0; i < n; i ++ )
        for ( j = i; j < n; j ++ )
            for ( k = j; k < n; k ++ ) {
                l.push_back( pereche( v[i], v[j], v[k] ) );
                p.push_back( &l.back() );
            }

    sort( p.begin(), p.begin() + p.size(), cmp );

    i = -1;
    j = p.size() - 1;
    do {
        i ++;
        while ( i <= j && p[i]->s + p[j]->s > s )
            j --;
    } while ( i <= j && p[i]->s + p[j]->s < s );

    if ( i <= j )
        fout << p[i]->a << ' ' << p[i]->b << ' ' << p[i]->c << ' '
             << p[j]->a << ' ' << p[j]->b << ' ' << p[j]->c << endl;
    else
        fout << -1;

    fin.close();
    fout.close();

    return 0;
}