Cod sursa(job #305909)

Utilizator danalex97Dan H Alexandru danalex97 Data 18 aprilie 2009 20:48:41
Problema Stramosi Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.82 kb
var m,n,p,q,i,v,j,c:longint;
    a,b:array[1..2500]of integer;
    f,g:text;
begin
assign(f,'stramosi.in');
reset(f);
assign(g,'stramosi.out');
rewrite(g);
read(f,n,m);
for i:=1 to n do begin
                   read(f,a[i]);
                   b[i]:=a[i];
                 end;
for i:=1 to n-1 do
  for j:=i+1 to n do
    if a[i]>a[j] then
      begin
        v:=a[i];
        a[i]:=a[j];
        a[j]:=v;
      end;
for i:=1 to m do
  begin
    read(f,q,p);
    j:=0;
    while a[i]<>b[q] do inc(c);
    while (j<p)and(c>1) do
	   begin
        dec(c);
		  if (a[c]<a[c+1])and(j>1) then inc(j);
        v:=a[c];
      end;
    if j=p then
      begin
		  j:=1;
        while b[j]<>a[c] do inc(j);
        writeln(g,j);
      end
           else writeln(g,'0');
  end;
close(f);
close(g);
end.