Cod sursa(job #1238945)

Utilizator dica69Alexandru Lincan dica69 Data 7 octombrie 2014 22:51:50
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 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 compare(sume x,sume y)
{return x.x<y.x;
}

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 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]);

for (i=1;i<=n;i++)
for (j=i;j<=n;j++)
for (z=j;z<=n;z++) {s[++m].x=a[i]+a[j]+a[z];s[m].p1=i;s[m].p2=j;s[m].p3=z;}
sort(s+1,s+m+1,compare);

for (i=m;i>=1;i--)
if (S-s[i].x>=0)
{p=caut(S-s[i].x);
if (p)
{fprintf(f2,"%ld %ld %ld %ld %ld %ld\n",a[s[i].p1],a[s[i].p2],a[s[i].p3],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.