|
var
a:array[1..2000000] of longint;
max,min:array[1..2000000,1..2] of longint;
answer1,answer2:array[1..2000000] of longint;
n,m,i,j,k,p,head1,tail1,head2,tail2:longint;
flag:boolean;
procedure insert1(x,time:longint);
var
l,r,mid:longint;
begin
l:=head1;
r:=tail1;
if l=r then begin
if x>=max[l,1] then begin
tail1:=l; max[l,1]:=x; max[l,2]:=time; end
else begin
inc(tail1);
max[tail1,1]:=x;
max[tail1,2]:=time;
end;
end
else begin
while l+1<r do
begin
mid:=(l+r) div 2;
if x>=max[mid,1] then r:=mid
else l:=mid;
end;
if x>=max[l,1] then begin
tail1:=l; max[l,1]:=x; max[l,2]:=time; end
else if x>=max[r,1] then begin
tail1:=r; max[r,1]:=x; max[r,2]:=time;
end
else begin
inc(tail1);
max[tail1,1]:=x;
max[tail1,2]:=time;
end;
end;
end;
procedure insert2(x,time:longint);
var
l,r,mid:longint;
begin
l:=head2;
r:=tail2;
if l=r then begin
if x<=min[l,1] then begin
tail2:=l; min[l,1]:=x; min[l,2]:=time; end
else begin
inc(tail2);
min[tail2,1]:=x;
min[tail2,2]:=time;
end;
end
else begin
while l+1<r do
begin
mid:=(l+r) div 2;
if x<=min[mid,1] then r:=mid
else l:=mid;
end;
if x<=min[l,1] then begin
tail2:=l; min[l,1]:=x; min[l,2]:=time; end
else if x<=min[r,1] then begin
tail2:=r; min[r,1]:=x; min[r,2]:=time;
end
else begin
inc(tail2);
min[tail2,1]:=x;
min[tail2,2]:=time;
end;
end;
end;
begin
fillchar(max,sizeof(max),0);
fillchar(min,sizeof(min),0);
readln(n,k);
head1:=1;
head2:=1;
tail1:=1;
tail2:=1;
for i:=1 to n do
read(a[i]);
max[tail1,1]:=a[1];
min[tail2,1]:=a[1];
max[tail1,2]:=1;
min[tail2,2]:=1;
for i:=2 to k do
begin
insert1(a[i],i);
insert2(a[i],i);
end;
answer1[1]:=max[1,1];
answer2[1]:=min[1,1];
for i:=k+1 to n do
begin
flag:=false;
for j:=head1 to tail1 do
if (i-max[j,2])<k
then begin flag:=true; break; end;
if flag=true then begin
head1:=j; insert1(a[i],i); end
else begin head1:=j+1; tail1:=head1;
max[tail1,1]:=a[i]; max[tail1,2]:=i; end;
flag:=false;
for j:=head2 to tail2 do
if (i-min[j,2])<k
then begin flag:=true; break; end;
if flag=true then begin
head2:=j; insert2(a[i],i); end
else begin head2:=j+1; tail2:=head2;
min[tail2,1]:=a[i]; min[tail2,2]:=i; end;
answer1[i-k+1]:=max[head1,1];
answer2[i-k+1]:=min[head2,1];
end;
write(answer2[1]);
for i:=2 to n-k+1 do
write(' ',answer2[i]);
writeln;
write(answer1[1]);
for i:=2 to n-k+1 do
write(' ',answer1[i]);
writeln;
end.
|
|