Cod sursa(job #1307341)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 1 ianuarie 2015 23:13:16
Problema Loto Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.58 kb
#include <fstream>
#include <unordered_map>
#include <vector>
using namespace std;

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

    ifstream f("loto.in");
    f >> N >> S;
    int a[N + 1], min = ~0;
    for (i = 1; i <= N; i++)
    {
        f >> a[i];
        if (a[i] < min)
            min = a[i];
    }
    f.close();

    bool found = false;
    if (S < 6 * min)
    {
        ofstream g("loto.out");
        g << -1;
    }
    else
    {
        int sum;
        vector<int> numbers;
        for (i = 0; i < 3; i++)
            numbers.push_back(0);

        unordered_map<int, vector<int>> map;
        for (i = 1; i <= N; i++)
            for (j = i; j <= N; j++)
                for (k = j; k <= N; k++)
                {
                    sum = a[i] + a[j] + a[k];
                    numbers[0] = a[i];
                    numbers[1] = a[j];
                    numbers[2] = a[k];
                    map[sum] = numbers;
                }

        unordered_map<int, vector<int>>::iterator pairIt;


        ofstream g("loto.out");

        int target;
        for (pairIt = map.begin(); pairIt != map.end() && !found; pairIt++)
        {
            target = S - pairIt->first;
            if (map.find(target) != map.end())
            {
                g << map[pairIt->first][0] << " " << map[pairIt->first][1] << " " << map[pairIt->first][2] << " ";
                g << map[S - pairIt->first][0] << " " << map[S - pairIt->first][1] << " " << map[S - pairIt->first][2];
                found = true;
            }
        }


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