Cod sursa(job #2127467)

Utilizator ZanoxNonea Victor Zanox Data 10 februarie 2018 18:00:08
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

#define lng long long

lng nr[100],i,j,k,l1,l2,l,st,n,s;

struct triplet
{
    lng nr[3],s;
};

triplet t[1000000];

int comp(triplet a,triplet b)
{
    if(a.s<b.s)return 1;
    return 0;
}

int main()
{
    fstream f("loto.in",ios::in),g("loto.out",ios::out);
    f>>n>>s;
    for(i=0;i<n;i++)f>>nr[i];
    for(i=0,l=0;i<n;i++)
        for(j=i;j<n;j++)
            for(k=j;k<n;k++,l++)
    {
        t[l].nr[0]=nr[i];
        t[l].nr[1]=nr[j];
        t[l].nr[2]=nr[k];
        t[l].s=nr[i]+nr[j]+nr[k];
    }
    st=l;
    sort(t,t+st,comp);
    for(i=0;i<st;i++)
    {
        l1=0;l2=st-1;
        while(l1!=l2)
        {
            l=(l1+l2)/2;
            if(t[i].s+t[l].s<s)l1=l+1;
            else l2=l;
        }
        if(t[i].s+t[l1].s==s)
        {
            for(j=0;j<3;j++)g<<t[i].nr[j]<<' ';
            for(j=0;j<3;j++)g<<t[l1].nr[j]<<' ';
            return 0;
        }
    }
    g<<-1;
    return 0;
}