Cod sursa(job #1861395)
Utilizator | Data | 28 ianuarie 2017 20:43:45 | |
---|---|---|---|
Problema | Cautare binara | Scor | 20 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 2.45 kb |
Program P1;
var fi,fo:text;
n,i,m,dr,st,a,mid,x:word;
s:array[1..100000] of word;
begin
assign(fi,'cautbin.in');
assign(fo,'cautbin.out');
reset(fi);
rewrite(fo);
readln(fi,n);
For i:=1 to n do
read(fi,s[i]);
readln(fi);
readln(fi,m);
For i:=1 to m do begin
read(fi,a,x);
readln(fi);
dr:=n;
st:=1;
If a=0 then begin
while st<=dr do begin
mid:=(st+dr) div 2;
If s[mid]<=x then st:=mid+1
else dr:=mid-1;
end;
mid:=(st+dr) div 2;
If s[mid]>x then dec(mid);
If s[mid]=x then writeln(fo,mid)
else writeln(fo,-1);
end
else If a=1 then begin
while st<dr do begin
mid:=(st+dr) div 2;
If s[mid]<=x then st:=mid+1
else dr:=mid;
end;
mid:=(st+dr) div 2;
If s[mid]>x then
writeln(fo,mid-1)
else writeln(fo,mid);
end
else If a=2 then begin
while st<dr do begin
mid:=(st+dr) div 2;
If s[mid]<x then st:=mid+1
else dr:=mid;
end;
mid:=(st+dr) div 2;
If s[mid]<x then
writeln(fo,mid+1)
else writeln(fo,mid);
end;
end;
close(fi);
close(fo);
end.