Cod sursa(job #2224482)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 24 iulie 2018 11:10:20
Problema Loto Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 105, MAXSUMS = 1000005;

struct perm{
    int s, a, b, c;
}sum[MAXSUMS];

int num[MAXN];

int n, target;

bool comp(perm x, perm y){
    return x.s <= y.s;
}

int main()
{
    ifstream fin("loto.in");
    ofstream fout("loto.out");
    fin >> n >> target;
    for(int i = 1; i <= n; ++i)
        fin >> num[i];
    int len = 0;
    for(int i = 1; i <= n; ++i){
        for(int j = i; j <= n; ++j){
            for(int k = j; k <= n; ++k){
                sum[++len].s = num[i] + num[j] + num[k];
                sum[len].a = num[i];
                sum[len].b = num[j];
                sum[len].c = num[k];
            }
        }
    }
    sort(sum + 1, sum + len + 1, comp);
    int found = 0;
    for(int i = 1; i <= len && !found; ++i){
        int dif = target - sum[i].s;
        int rez = 0, pas = 1 << 8;
        while(pas){
            if(rez + pas <= n && sum[rez + pas].s < dif)
                rez += pas;
            pas /= 2;
        }
        rez++;
        if(sum[i].s + sum[rez].s == target){
            fout << sum[i].a << " " << sum[i].b << " " << sum[i].c << " " << sum[rez].a << " " << sum[rez].b << " " << sum[rez].c << "\n";
            found = 1;
        }
    }
    if(!found)
        fout << -1;
    return 0;
}