Cod sursa(job #1415413)

Utilizator stanciuandreiStanciulescu Andrei stanciuandrei Data 4 aprilie 2015 15:49:19
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("loto.in");
ofstream out("loto.out");
int sump[1000000], index, n;
bool binar(int x)
{
    int l=0,r=n-1;
    bool found=false;
    while(l<r&&!found)
    {
        if(sump[(l+r)/2]==x)
            return true;
        else
        {
            if(sump[(l+r)/2]>x)
                r=(l+r)/2+1;
            else
                l=(l+r)/2+1;
        }
    }
    return false;
}
int main()
{
    int s;
    in>>n>>s;
    int numere[n];
    for(int i=0;i<n;i++)
    {
        in>>numere[i];
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<n;k++)
            {
                sump[index]=numere[i]+numere[k]+numere[j];
                index++;
            }
        }
    }
    sort(sump, sump+index);
    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            for(int k=j;k<n;k++)
            {
                int x;
                x=s-numere[i]-numere[j]-numere[k];
                if(binar(x))
                {
                    for(int a=0;a<n;a++)
                    {
                        for(int b=0;b<n;b++)
                        {
                            for(int c=0;c<n;c++)
                            {
                                if(numere[a]+numere[b]+numere[c]==x)
                                {
                                    out<<numere[i]<<" "<<numere[i]<<" "<<numere[k]<<" ";
                                    out<<numere[a]<<" "<<numere[b]<<" "<<numere[c]<<"\n";
                                    return 0;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    out<<-1<<"\n";
    return 0;
}