Cod sursa(job #290412)

Utilizator lsorin_94Lodoaba Sorin lsorin_94 Data 27 martie 2009 21:51:34
Problema Loto Scor 0
Compilator fpc Status done
Runda scratch.the.answer Marime 1.63 kb
program int;
type vector=array [1..9047382] of longint;
var  suma,x:vector;
n,m,i,j,k,s,st,dr,mij:longint;
ok:boolean;
procedure sortare(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
      inc(i);
    while x<a[j] do
      dec(j);
    if i<=j then
  begin
    y:=a[i];
    a[i]:=a[j];
    a[j]:=y;
    inc(i);
    dec(j);
  end;
  until i>j;
    if l<j then
      sortare(a,l, j);
    if i<r then
      sortare(a,i, r);
  end;
  procedure afisare(p:longint);
  var i,j,l,q:longint;
begin
  for i:=1 to n do
    for j:=1 to n do
      for l:=1 to n do
        if (x[i]+x[j]+x[l]=suma[p]) then
          begin
           write(output,x[i],' ',x[j],' ',x[l],' ');
           exit;
          end;
end;
begin
  assign(input,'loto.in');
  reset(input);
  readln(input,n,s);
  for i:=1 to n do
    read(input,x[i]);
  close(input);
  m:=0;
  for i:=1 to n do
    for j:=1 to n do
      for l:=1 to n do begin
         inc(m);
  suma[m]:=x[i]+x[j]+x[l];
end;
  sortare(suma,1,m);
  ok:=false;
  st:=1;
  dr:=m;
  assign(output,'loto.out');
  rewrite(output);
  while st<=dr do
    begin
      mij:=(st+dr) div 2;
      if suma[st]+suma[dr]=s then
         begin
           ok:=true;
           afisare(st);
           afisare(dr);
           st:=dr+1;
         end
      else
        begin
          while (suma[st]+suma[dr]>s) and (dr>=1) do
            dec(dr);
          while (suma[st]+suma[dr]<s) and (st<=m) do
            inc(st);
        end;
    end;
  if not ok then
    write(output,-1);
  close(output);
end.