Cod sursa(job #1528706)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 19 noiembrie 2015 22:44:33
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include<cstdio>
#include<algorithm>
using namespace std;
struct chestie{int s,a,b,c;};
chestie v[1000010];
int nr[110];
bool cmp(chestie a,chestie b){
    if(a.s<b.s)
        return true;
    return false;
}
int main(){
    freopen("loto.in","r",stdin);
    freopen("loto.out","w",stdout);
    int n,i,j,k,cnt=0,s,x,l1,l2,m;
    scanf("%d%d",&n,&s);
    for(i=1;i<=n;i++)
        scanf("%d",&nr[i]);
    for(i=1;i<=n;i++)
        for(j=i;j<=n;j++)
                for(k=i;k<=n;k++){
                    cnt++;
                    v[cnt].s=nr[i]+nr[j]+nr[k];
                    v[cnt].a=nr[i];
                    v[cnt].b=nr[j];
                    v[cnt].c=nr[k];
                }
    sort(v+1,v+cnt+1,cmp);
    for(j=1;j<=cnt;j++){
        x=s-v[j].s;
        if(x<0)
            break;
        l1=j;
        l2=cnt;
        while(l1<=l2){
            m=(l1+l2)/2;
            if(v[m].s==x)
                break;
            if(v[m].s<x)
                l1=m+1;
            else
                l2=m-1;
        }
        if(l1<=l2){
            printf("%d %d %d %d %d %d",v[j].a,v[j].b,v[j].c,v[m].a,v[m].b,v[m].c);
            return 0;
        }
    }
    printf("-1");
    return 0;
}