Cod sursa(job #1238922)

Utilizator dica69Alexandru Lincan dica69 Data 7 octombrie 2014 22:20:59
Problema Loto Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
#include <cstdio>
#include <algorithm>


using namespace std;

struct sume{long x,p1,p2,p3;};
FILE *f1,*f2;
long n,S,a[101],i,j,z,m,p,ok=0;
sume s[1000000];

int caut(int x)
{int t,p,u;
p=1;u=m;
while (p<=u)
{t=(p+u)/2;
if (x==s[t].x) return t;
else if (x<s[t].x) u=t-1;
else p=t+1;
}
return 0;
}

int unic(int x)
{int i;
for (i=1;i<=m;i++) if(s[i].x==x) return 0;
return 1;
}


int main()
{f1 = fopen("loto.in","r");
f2 = fopen("loto.out","w");
fscanf(f1,"%ld%ld",&n,&S);
for (i=1;i<=n;i++) fscanf(f1,"%ld",&a[i]);
sort(a+1,a+n+1);
for (i=1;i<=n;i++)
for (j=i;j<=n;j++)
for (z=j;z<=n;z++) if(unic(a[i]+a[j]+a[z])) {s[++m].x=a[i]+a[j]+a[z];s[m].p1=i;s[m].p2=j;s[m].p3=z;}

for (i=1;i<=n;i++)
if (!ok)
for (j=i;j<=n;j++)
if (!ok)
for (z=j;z<=n;z++)
if (S-(a[i]+a[j]+a[z])>=0)
{p=caut(S-(a[i]+a[j]+a[z]));
if (p)
{fprintf(f2,"%ld %ld %ld %ld %ld %ld\n",a[i],a[j],a[z],a[s[p].p1],a[s[p].p2],a[s[p].p3]);
ok=1;
break;
}
}

if (!ok) fprintf(f2,"-1\n");
fclose(f1);fclose(f2);
    return 0;
}

//Challenges are what make life interesting and overcoming them is what makes life meaningful.