Cod sursa(job #1238904)

Utilizator dica69Alexandru Lincan dica69 Data 7 octombrie 2014 22:03:45
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <windows.h>

using namespace std;

struct sume{long x,p1,p2,p3;};
FILE *f1,*f2;
long n,S,a[101],i,j,z,m,p;
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=1;j<=n;j++)
for (z=1;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",s[i].p1,s[i].p2,s[i].p3,s[p].p1,s[p].p2,s[p].p3);
fclose(f2);
exit(0);
}
}

fprintf(f2,"-1");
fclose(f1);fclose(f2);
    return 0;
}

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