Cod sursa(job #1147785)

Utilizator vladvaldezVlad Dimulescu vladvaldez Data 20 martie 2014 09:48:38
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <algorithm>
#include <stdio.h>

using namespace std;
FILE *f=fopen("loto.in","r");
FILE *g=fopen("loto.out","w");

int n,v[105],s,i,p,nr,ok,c[7],k;

int cautb(int x)
{int m,p=1,u=n,mx=999999;

while(p<=u)
{
m=(p+u)/2;
if (v[m]>x)
   {
     if (m<mx)mx=m;
     u=m-1;
   }
else p=m+1;
}
return mx;
}

int main()
{
fscanf(f,"%d%d",&n,&s);
for(i=1;i<=n;i++)
fscanf(f,"%d",&v[i]);
sort(v+1,v+n+1);
p=1;
nr=6;
ok=1;
while (ok)
{

if (v[n]*(nr-1)>=s-v[p])
{
c[++k]=v[p];
nr--;
s=s-v[p];
}
else p=cautb(v[p]);
if (p==999999||k==6)ok=0;
}
if (k==6)for(i=1;i<=6;i++) fprintf(g,"%d ",c[i]);
else fprintf(g,"%d",-1);
fclose(g);
return 0;
}