Cod sursa(job #1306994)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 31 decembrie 2014 19:36:26
Problema Loto Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <unordered_map>
using namespace std;

struct Node
{
    int indexI, indexJ, indexK;
};

int main()
{
    int N, S, i, j, k;

    ifstream f("loto.in");
    f >> N >> S;
    int a[N + 1];
    for (i = 1; i <= N; i++)
        f >> a[i];
    f.close();

    unordered_map<int, Node> map;
    int sum;
    for (i = 1; i <= N; i++)
        for (j = 1; j <= N; j++)
            for (k = 1; k <= N; k++)
            {
                sum = a[i] + a[j] + a[k];
                if (sum <= S)
                {
                    Node newEntry;
                    newEntry.indexI = i;
                    newEntry.indexJ = j;
                    newEntry.indexK = k;
                    map[sum] = newEntry;
                }
            }

    bool found = false;
    unordered_map<int, Node>::iterator pairIt;

    ofstream g("loto.out");
    for (unordered_map<int, Node>::iterator it = map.begin(); it != map.end() && !found; it++)
    {
        pairIt = map.find(S - it->first);
        if (pairIt != map.end())
        {
            found = true;
            g << a[it->second.indexI] << " " << a[it->second.indexJ] << " " << a[it->second.indexK] << " ";
            g << a[pairIt->second.indexI] << " " << a[pairIt->second.indexJ] << " " << a[pairIt->second.indexK];
        }
    }

    if (!found)
        g << -1;
    g.close();

    return 0;
}