Cod sursa(job #3242997)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 14 septembrie 2024 23:01:26
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.46 kb
#include <deque>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <bitset>
#define pb push_back
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
typedef int ll;
typedef pair<int, int> pi;
int t, T;


struct info {
    ll x1, x2, x3, comp_sum;

    void to_string() {
        g << x1 << ' ' << x2 << ' ' << x3 << ' ';
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n, s;
    f >> n >> s;
    vector<ll> v;
    for (int i = 0; i < n; i++) {
        ll x;
        f >> x;
        v.push_back(x);
    }

    // check for all 6 identical
    for (ll x : v) {
        if (6LL * x == s) {
            for (int i = 0; i < 6; i++) {
                g << x << ' ';
            }
            return 0;
        }
    }
    
    // check 5 identical and one distinct
    v.resize(v.size());
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != j && v[i] * 5LL + v[j] == s) {
                for (int k = 0; k < 5; k++) {
                    g << v[i] << ' ';
                }

                g << v[j];
                return 0;
            }
        }
    }
    
    vector<ll> a;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 1; j++) {
            a.push_back(v[i]);
        }
    }
    a.resize(a.size());

    vector<info> all_triplets;
    for (int i = 0; i < a.size(); i++) {
        for (int j = i + 1; j < a.size(); j++) {
            for (int k = j + 1; k < a.size(); k++) {
                all_triplets.push_back({ a[i], a[j], a[k], a[i] + a[j] + a[k] });
            }
        }
    }

    sort(all_triplets.begin(), all_triplets.end(), [](const info& X, const info& Y) {
            
            return X.comp_sum < Y.comp_sum;
        });

    all_triplets.resize(all_triplets.size());
    int left = 0, right = all_triplets.size() - 1;
    while (left <= right) {
        ll local_sum = all_triplets[left].comp_sum + all_triplets[right].comp_sum;
        if (local_sum == s) {
            all_triplets[left].to_string();
            all_triplets[right].to_string();
            return 0;
        }

        if (local_sum > s) {
            right--;
        }
        else {
            left++;
        }
    }
    g << -1;
    return 0;
}