Nu aveti permisiuni pentru a descarca fisierul grader_test47.ok
Cod sursa(job #221874)
Utilizator | Data | 18 noiembrie 2008 18:06:29 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 1.03 kb |
var c,loc,i,n,m,b:longint;
vec:array[1..100000] of longint;
f,f2:text;
a:byte;
function cautbin(st,dr,k:longint):longint;
var mij:longint;
begin
mij:=(st+dr) div 2;
if k=vec[mij] then
begin
cautbin:=mij;
end
else
begin
if (st>=dr) then
begin
cautbin:=-1;
loc:=mij;
end
else
begin
if k<vec[mij] then
begin
cautbin:=cautbin(st,mij-1,k);
end
else
begin
cautbin:=cautbin(mij+1,dr,k);
end;
end;
end;
end;
begin
loc:=0;
assign(f,'cautbin.in');
assign(f2,'cautbin.out');
reset(f);
rewrite(f2);
readln(f,n);
for i:=1 to n do read(f,vec[i]);
readln(f,m);
for i:=1 to m do
begin
read(f,a,b);
if a=0 then writeln(f2,cautbin(1,n,b))
else
begin
c:=cautbin(1,n,b);
if c=-1 then
begin
if a=1 then c:=loc;
if a=2 then c:=loc+1;
end;
writeln(f2,c);
end;
end;
close(f);
close(f2);
writeln(cautbin(1,n,7));
end.