Cod sursa(job #1322053)

Utilizator priestnoobFMI - Dan Nema priestnoob Data 19 ianuarie 2015 19:09:07
Problema Loto Scor 90
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.62 kb
#include<fstream>
#include<unordered_set>
#include<algorithm>

using namespace std;

#define nmax 101

unordered_set<int>h;
int v[nmax],n,s;
bool c;

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

void citire()
{
    f>>n>>s;
    for(int i=1;i<=n;++i) f>>v[i];
}

void solve()
{
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            for(int k=1;k<=n;++k)
                if (h.find(v[i] + v[j] + v[k]) == h.end())
                {
                    h.insert(v[i] + v[j] + v[k]);
                }
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=n;++j)
        {
            for(int k=1;k<=n;++k)
            {
                if (h.find(s - (v[i] + v[j] + v[k])) != h.end())
                {
                    g<<v[i]<<' '<<v[j]<<' '<<v[k]<<' ';
                    s -= v[i] + v[j] + v[k];
                    c = 1;
                    break;
                }
            }
            if(c) break;
        }
        if(c) break;
    }
    if(!c) printf("-1");
    else
    {
        c=0;
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                for(int k=1;k<=n;++k)
                if (v[i] + v[j] + v[k] == s)
                {
                    g<<v[i]<<' '<<v[j]<<' '<<v[k];
                    s -= v[i] + v[j] + v[k];
                    c = 1;
                    break;
                }
                if(c) break;
            }
            if(c) break;
        }
    }
}

int main()
{
    freopen("loto.in","r",stdin);
    freopen("loto.out","w",stdout);
    citire();
    solve();
}