Cod sursa(job #209270)
Utilizator | Data | 21 septembrie 2008 17:55:40 | |
---|---|---|---|
Problema | Cautare binara | Scor | 60 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 2.43 kb |
program pascal;
var f,g:text;
st,dr,i,n,p,q,t,m,pos:longint;
v:array[1..100000] of longint;
ok,bun:boolean;
procedure florin;
begin
st:=1;
dr:=n;
ok:=false;
repeat
q:=st+(dr-st) div 2;
if p>v[q] then st:=(st+dr) div 2
else
if p<v[q] then dr:=(st+dr) div 2
else
if p=v[q] then begin
ok:=true;
pos:=q;
end;
until (ok) or (st+1=dr);
if st+1=dr then
begin
if p=v[st] then
begin
ok:=true;
pos:=st;
end
else
if p=v[dr] then
begin
ok:=true;
pos:=dr;
end;
end;
end;
begin
assign(f,'cautbin.in'); reset(f);
assign(g,'cautbin.out'); rewrite(g);
readln(f,n);
for i:=1 to n do read(f,v[i]);
readln(f);
read(f,m);
for i:=1 to m do
begin
readln(f,t,p);
if t=0 then
begin
florin;
if ok then writeln(g,pos)
else writeln(g,-1);
end
else
if t=1 then begin
florin;
if ok then writeln(g,pos)
else
if (p>v[st]) and (p<v[dr]) then writeln(g,st)
else writeln (g,dr);
end
else
if t=2 then begin
florin;
if ok then writeln(g,pos)
else
if (p>v[st]) and (p<v[dr]) then writeln(g,dr)
else writeln(g,st);
end;
end;
close(f);
close(g);
end.