Cod sursa(job #2224491)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 24 iulie 2018 11:28:36
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 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 cauta(int difer, int lim){
    int st = 1, dr = lim, mij;
    while(st <= dr){
        mij = (st + dr) / 2;
        if(sum[mij].s < difer)
            st = mij + 1;
        else if(sum[mij].s > difer)
            dr = mij - 1;
        else
            return mij;
    }
    return -1;
}

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);
    bool found = 0;
    for(int i = 1; i <= len && !found; ++i){
        int dif = target - sum[i].s, rez = cauta(dif, len);
        if(rez != -1){
            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;
}