Cod sursa(job #884018)

Utilizator PavelPavel Ana-Oriana Pavel Data 20 februarie 2013 16:45:44
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <cstdio>
#include <algorithm>

using namespace std;
int v[103],sume[1000001];
struct numere
{
    int x,y,z;
};
numere r[1000001];

int cautbin(int suma,int n)
{
    int i,pas=1<<20;
    for(i=0 ; pas != 0 ; pas/=2)
        if((i+pas) <= n && sume[i+pas] <= suma)
            i+=pas;
    return i;
}
int main()
{
    FILE *in = fopen("loto.in","r");
    FILE *out = fopen("loto.out","w");
    int n,sum,size=0,i,j,k,t;
    fscanf(in,"%d%d",&n,&sum);
    for(i=1 ; i<=n ; i++)
        fscanf(in,"%d",&v[i]);
    for(i=1 ; i<=n ; i++)
        for(j=i ; j<= n ; j++)
            for(k=j ; k<=n ; k++){
                size++;
                r[size].x=v[i];
                r[size].y=v[j];
                r[size].z=v[k];
                sume[size]=v[i]+v[j]+v[k];
            }
    sort(sume + 1 , sume + size + 1 );
    for(i=1 ; i <= size ; i++)
    {
        t=cautbin(sum-sume[i],size);
        if(sume[i]+sume[t] == sum)
        {
            fprintf(out,"%d %d %d %d %d %d\n",r[i].x,r[i].y,r[i].z,r[t].x,r[t].y,r[t].z);
            return 0;
        }
    }
    fprintf(out,"-1\n");
    return 0;
}