Cod sursa(job #1711531)

Utilizator valentinoMoldovan Rares valentino Data 31 mai 2016 16:12:30
Problema Loto Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.41 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#define N 1003001
using namespace std;

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

int n1, n, s, a[101], v[N], sol[7], nsol;

void show(int sum)
{
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            for(int k = 1; k <= n; ++k)
            {
                if(a[i] + a[j] + a[k] == sum)
                 {
                     sol[++nsol] = a[i];
                     sol[++nsol] = a[j];
                     sol[++nsol] = a[k];
                     return;
                 }
            }
}

int main()
{
    f >> n >> s;
    for(int i = 1; i <= n; ++i)
        f >> a[ i ];
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            for(int k = 1; k <= n; ++k)
                v[ ++n1 ] = a[ i ] + a [ j ] + a[ k ];
    sort(v + 1, v + n1 + 1);
    int st = 1;
    int dr = n1;
    bool gasit = false;
    while(st <= dr)
    {
        if(v[st] + v[dr] == s)
        {
            show(v[st]);
            show(v[dr]);
            gasit = true;
            break;
        }
        else if(v[st] + v[dr] < s)
            st++;
        else dr--;
    }
    if(gasit)
    {
        //sort(sol + 1, sol + nsol + 1);
        for(int i = 1; i <= nsol; ++i)
            g<< sol[i] << ' ';
        //g << '\n';
        return 0;
    }
    g << -1;
    return 0;

}