Cod sursa(job #1072024)

Utilizator DjokValeriu Motroi Djok Data 3 ianuarie 2014 20:31:57
Problema Cautare binara Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 1.8 kb
var  n,i,j,m,temp,nr,poz,left,right:longint;
     a:array[1..100001] of longint;

begin
assign(input,'cautbin.in');
assign(output,'cautbin.out');
reset(input);
rewrite(output);

   readln(n);
    for i:=1 to n do
     read(a[i]);

  readln(m);
     for i:=1 to m do
      begin
       left:=1; right:=n;  poz:=(left+right) div 2;
       read(temp); readln(nr);
       if temp=0 then begin
                       while left<=right do
                        begin
                          if a[poz]<=nr then left:=poz+1
                                        else right:=poz-1;
                               poz:=(left+right) div 2;
                        end;

                        if a[poz]>nr then dec(poz);
                        if a[poz]=nr then  writeln(poz)
                                     else  writeln('-1');
                      end;

       if temp=1 then begin
                       while left<right do
                        begin
                         if a[poz]<=nr then left:=poz+1
                                      else right:=poz;
                         poz:=(left+right) div 2;
                        end;

                         if a[poz]>nr then dec(poz);
                           writeln(poz);
                      end;


       if temp=2 then  begin
                         while left<right do
                          begin
                           if a[poz]<nr then left:=poz+1
                                        else right:=poz;
                           poz:=(left+right) div 2;
                          end;
                               if a[poz]<nr then inc(poz);
                            writeln(poz);
                       end;
      end;

close(input);
close(output);
{Si totusi este trist in lume.}
end.