Cod sursa(job #1508604)

Utilizator radu_uniculeu sunt radu radu_unicul Data 22 octombrie 2015 19:15:11
Problema Loto Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include<stdio.h>
#include<vector>
#include<algorithm>
#define N 101*101*101
using namespace std;
int n,s,i,j,k,key,it;
struct chestie
{
    int a;
    int b;
    int c;
    int sum;
};
bool comp(chestie a,chestie b)
{
    return a.sum<b.sum;
}
chestie ch[N];
chestie val;
int v[101];
bool bs(int x,chestie ches)
{
    int pas=1<<26,start=0,end=it;
    //printf("BS de %d:\n",x);
    for(pas; pas>0; pas=pas>>1)
    {
        if(start+pas>=end) continue;
        //printf("Se cauta in:%d Valoare:%d\n",start+pas,ch[start+pas].sum);
        if(ch[start+pas].sum<x) start+=pas;
        else if(ch[start+pas].sum==x)
        {
            printf("%d %d %d %d %d %d",ch[start+pas].a,ch[start+pas].b,ch[start+pas].c,ches.a,ches.b,ches.c);
            return 1;
        }
    }
    return 0;
}
int main()
{
    freopen("loto.in","r",stdin);
    freopen("loto.out","w",stdout);
    scanf("%d%d",&n,&s);
    for(i=0; i<n; i++) scanf("%d",&v[i]);
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            for(k=0; k<n; k++)
            {
                ch[it].a=v[i];
                ch[it].b=v[j];
                ch[it].c=v[k];
                ch[it].sum=v[i]+v[j]+v[k];
                ++it;
            }
    sort(ch,ch+it,comp);
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            for(k=0; k<n; k++)
            {
                chestie che;
                che.a=v[i];
                che.b=v[j];
                che.c=v[k];
                che.sum=v[i]+v[j]+v[k];
                if(bs(s-che.sum,che)) return 0;
            }
    printf("-1");
    return 0;
}