Cod sursa(job #633510)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 13 noiembrie 2011 21:54:41
Problema Loto Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.66 kb
Program loto;
type tip=record
    s,x,y,z:longint;
      end;
 var a:array [0..100] of longint;
    b:array [0..500000] of tip;
    b1,b2:array [1..1 shl 10] of char;
    i,j,k,n,t,g,s:longint;
    fi,fo:text;
procedure sort(l,r:longint);
 var i,j,k:longint;
      y:tip;
begin
 i:=l; j:=r; k:=b[(l+r) div 2].s;
 repeat
  while k>b[i].s do inc(i);
   while k<b[j].s do dec(j);
  if i<=j then begin
              y:=b[i]; b[i]:=b[j]; b[j]:=y;
              inc(i); dec(j);
              end;
 until i>=j;
 if i<r then sort(i,r);
  if j>l then sort(l,j);
end;
procedure cauta(l,r:longint);
 var mid:longint;
begin
g:=-1;
 while l<=r do begin
              mid:=(l+r) div 2;
              if b[mid].s>s-b[i].s then r:=mid-1
                else if b[mid].s<s-b[i].s then l:=mid+1
                  else begin g:=mid; l:=mid+1; end;
                end;
end;
begin
 assign(fi,'loto.in');
  assign(fo,'loto.out');
 settextbuf(fi,b1); settextbuf(fo,b2);
 reset(fi); rewrite(fo);
 readln(fi,n,s);
 for i:=1 to n do read(fi,a[i]);
  for i:=1 to n do
   for j:=i to n do
    for k:=j to n do
     if a[i]+a[j]+a[k]<s then begin
                      inc(t); b[t].s:=a[i]+a[j]+a[k];
                       b[t].x:=a[i]; b[t].y:=a[j]; b[t].z:=a[k];
                      end;
  sort(1,t);
 for i:=1 to t do begin
                    cauta(1,t);
                     if g<>-1 then begin
                         write(fo,b[g].x,' ',b[g].y,' ',b[g].z,' ',b[i].x,' ',b[i].y,' ',b[i].z);
                         close(fo);
                          exit;
                           end;
                    end;
 write(fo,'-1');
close(fo);
end.