Cod sursa(job #1307001)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 31 decembrie 2014 19:40:00
Problema Loto Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 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 && !map[sum])
                {
                    Node *newEntry = new Node;
                    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;
}