Cod sursa(job #2263532)

Utilizator vladboss2323Ciorica Vlad vladboss2323 Data 18 octombrie 2018 19:20:26
Problema Loto Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

ifstream in("loto.in");
ofstream out("loto.out");

const int N=105;

int n,smax,a[N],nr;

struct Suma
{
    int suma,x,y,z;
};

Suma sume[N*N*N];

bool cmp(Suma A,Suma B)
{
    return A.suma<B.suma;
}

int caut_bin(int S)
{
    int pas=1<<20,rez=0;
    while(pas)
    {
        if(rez+pas<=nr && sume[rez+pas].suma <= S)
            rez=rez+pas;
        pas=pas/2;
    }
    if(sume[rez].suma != S)
        return 0;
    else
        return rez;
}

int main()
{
    int i,j,q,rez;
    in>>n>>smax;
    for(i=1; i<=n; i++)
        in>>a[i];
    sort(a+1,a+n+1);
    if(a[n]*6<smax)
        out<<-1;
    else
    {
        int ok=0;
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                for(q=1; q<=n; q++)
                    sume[++nr]= {a[i]+a[j]+a[q],a[i],a[j],a[q]};
        sort(sume+1,sume+nr+1,cmp);
        for(i=1; i<= nr; i++)
        {
            rez=caut_bin(smax-sume[i].suma);
            if(rez!=0 && ok==0)
            {
                out<<sume[i].x<<" "<<sume[i].y<<" "<<sume[i].z<<" "<<sume[rez].x<<" "<<sume[rez].y<<" "<<sume[rez].z;
                ok=1;
            }

        }
        if(ok==0)
            out<<-1;

    }
    return 0;
}