Cod sursa(job #771172)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 25 iulie 2012 00:29:35
Problema Semne Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.32 kb
Program semne;
 var a,plus,minus,sol:array [0..505] of longint;
     i,n,sc,s,p,m,aux,pos:longint;
     ok:boolean;
     fi,fo:text;
procedure cauta(val:longint);
 var l,r,mid:longint;
begin
 pos:=-1; l:=1; r:=n;
  while (pos=-1) and (l<=r) do begin
   mid:=(l+r) div 2;
   if a[mid]>val then r:=mid-1
   else if a[mid]<val then l:=mid+1
   else pos:=mid;
  end;
end;
begin
 assign(fi,'semne.in');
  assign(fo,'semne.out');
 reset(fi); rewrite(fo); readln(fi,n,s); sc:=0;
  for i:=1 to n do begin
                    read(fi,a[i]);
                    aux:=random(2);
                    if aux=0 then begin inc(m); sc:=sc-a[i]; minus[m]:=a[i]; end
                                else begin inc(p); sc:=sc+a[i]; plus[p]:=a[i]; end;
                    end;
  while sc<>s do
    if sc<s then begin
                  aux:=random(m)+1;
                  inc(p); plus[p]:=minus[aux];
                  sc:=sc+2*minus[aux];
                  minus[aux]:=minus[m]; dec(m);
                  end
    else begin
          aux:=random(p)+1;
          inc(m); minus[m]:=plus[aux];
          sc:=sc-2*plus[aux];
          plus[aux]:=plus[p]; dec(p);
         end;
  for i:=1 to m do begin cauta(minus[i]); sol[pos]:=1; end;
   for i:=1 to n do
    if sol[i]=1 then write(fo,'-') else write(fo,'+');
 close(fo);
end.