Cod sursa(job #1711525)

Utilizator valentinoMoldovan Rares valentino Data 31 mai 2016 16:06:02
Problema Loto Scor 5
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.07 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];

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)
                    g << a[i] << ' ' << a[j] << ' ' << 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;
    while(st <= dr)
    {
        if(v[st] + v[dr] == s)
        {
            show(v[st]);
            show(v[dr]);
            return 0;
        }
        else if(v[st] + v[dr] < s)
            st++;
        else dr--;
    }
    g << -1;
    return 0;

}