Cod sursa(job #324658)

Utilizator marius21Marius Petcu marius21 Data 16 iunie 2009 17:07:12
Problema Cautare binara Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.35 kb
var f,g:text;
n,i,x,p,u,m,op:longint;
a:array[1..100001] of longint;
bufin:array[0..100001] of byte;
begin
assign(f,'cautbin.in');
assign(g,'cautbin.out');
reset(f);
rewrite(g);
settextbuf(f,bufin);
read(f,n);
for i:=1 to n do
        read(f,a[i]);
read(f,m);
for i:=1 to m do begin
        read(f,op,x);
        p:=1;
        u:=n;
        if op=0 then
        while p<=u do begin
                m:=p+((u-p) shr 1);
                if a[m]>x then
                        u:=m-1
                        else
                        p:=m+1;
                end
        else
        if op=1 then
        while p<=u do begin
                m:=p+((u-p) shr 1);
                if a[m]>x then
                        u:=m-1
                        else
                        p:=m+1;
                end;
        if op=2 then
        while p<=u do begin
                m:=p+((u-p) shr 1);
                if a[m]<x then
                        p:=m+1
                        else
                        u:=m-1;
                end;
        case op of
                0:
                if a[u]=x then
                        writeln(g,u)
                        else
                        writeln(g,-1);

                1:writeln(g,u);
                2:writeln(g,p);
                end;
        end;
close(f);
close(g);
end.