Cod sursa(job #182432)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 20 aprilie 2008 21:36:01
Problema Loto Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.92 kb
 Program cel;
 Type vector=array[1..1000000] of longint;
 var f,g:text;
     suma,x:vector;
     n,m,i,j,k,s,ic,sf,mij:longint;
     ok:boolean;

 procedure Sort(var a:vector;l, r: longint);
 var
   i, j, x, y: longint;
 begin
   i := l; j := r; x := a[(l+r) DIV 2];
   repeat
     while a[i] < x do i := i + 1;
     while x < a[j] do j := j - 1;
     if i <= j then
     begin
       y := a[i]; a[i] := a[j]; a[j] := y;
       i := i + 1; j := j - 1;
     end;
   until i > j;
   if l < j then Sort(a,l, j);
   if i < r then Sort(a,i, r);
 end;


 procedure afisare(p:longint);
 var i,j,k,e:longint;
 Begin
 e:=0;
 For i:=1 to n do
  For j:=1 to n do
   For k:=1 to n do
    If (x[i]+x[j]+x[k]=suma[p]) and (e=0) then Begin
                                                Write(g,x[i],' ',x[j],' ',x[k],' ');
                                                inc(e);
                                                break;
                                               end;
 end;

 Begin
 Assign(f,'loto.in');Reset(f);
 Assign(g,'loto.out');Rewrite(g);
 Readln(f,n,s);
 For i:=1 to n do Read(f,x[i]);
 m:=0;
  For i:=1 to n do
  For j:=1 to n do
     For k:=1 to n  do
   Begin
     m:=m+1;
     suma[m]:=x[i]+x[j]+x[k];
    end;
 Sort(suma,1,m);
 ok:=false;
 ic:=1;
 sf:=m;
  While ic<=sf do
   Begin
    mij:=(ic+sf) div 2;
    If suma[ic]+suma[sf]=S then Begin
                                 ok:=true;
                                 afisare(ic);
                                 afisare(sf);
                                 ic:=sf+1;
                                end
                            else Begin
                                  While (suma[ic]+suma[sf]>S) and (sf>=1) do dec(sf);
                                  While (suma[ic]+suma[sf]<S) and (ic<=m) do inc(ic);
                                 end;
    end;
 If not ok then Write(g,-1);
 Close(f);
  Close(g);
 end.