Wikioi 4373 窗口(单调队列)
2016-11-16 10:23
232 查看
4373 窗口
时间限制: 1 s
空间限制: 256000 KB
题目等级 : 黄金 Gold
题解
查看运行结果
题目描述 Description
给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:
Window position Min value
Max value
[ 1 3 -1 ] -3 5 3 6 7
-1 3
1 [ 3 -1 -3 ] 5 3 6 7 -3 3
1 3 [ -1 -3 5 ] 3 6 7 -3 5
1 3 -1 [ -3 5 3 ] 6 7 -3 5
1 3 -1 -3 [ 5 3 6 ] 7 3 6
1 3 -1 -3 5 [ 3 6 7 ] 3 7
你的任务是找出窗口在各位置时的max value, min value.
输入描述 Input Description
第1行n,k,第2行为长度为n的数组
输出描述 Output Description
2行
第1行每个位置的min value
第2行每个位置的max value
样例输入 Sample Input
8 3
1 3 -1 -3 5 3 6 7
样例输出 Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
数据范围及提示 Data Size & Hint
数据范围:20%: n<=500; 50%: n<=100000;100%: n<=1000000;
program df;
var i,j,n,m,z,l,r,l1,r1:longint;
a,b1,c1,b2,c2,x,y:array[0..3000000] of longint;
begin
readln(n,m);
for i:=1 to n do
read(a[i]);
l:=1; r:=0;
l1:=1; r1:=0;
for i:=1 to m do //先把前m个元素放入
begin
while (l<=r) and (b1[r]<=a[i]) do dec(r);
inc(r);
b1[r]:=a[i];
c1[r]:=i;
while (l1<=r1) and (b2[r1]>=a[i]) do dec(r1);
inc(r1);
b2[r1]:=a[i];
c2[r1]:=i;
end;
x[m]:=b1[l];
y[m]:=b2[l1];
for i:=m+1 to n do
begin
while (l<=r) and (b1[r]<=a[i]) do dec(r); //保证栈递增
inc(r);
b1[r]:=a[i];
c1[r]:=i;
while (l<=r) and (c1[l]<=i-m) do inc(l); //保证单调栈的区间长度等于M
while (l1<=r1) and (b2[r1]>=a[i]) do dec(r1); //保证栈递减
inc(r1);
b2[r1]:=a[i];
c2[r1]:=i;
while (l1<=r1) and (c2[l1]<=i-m) do inc(l1); //和上面一样
x[i]:=b1[l];
y[i]:=b2[l1];
end;
for i:=m to n do
write(y[i],’ ‘);
writeln;
for i:=m to n do
write(x[i],’ ‘);
end.
时间限制: 1 s
空间限制: 256000 KB
题目等级 : 黄金 Gold
题解
查看运行结果
题目描述 Description
给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:
Window position Min value
Max value
[ 1 3 -1 ] -3 5 3 6 7
-1 3
1 [ 3 -1 -3 ] 5 3 6 7 -3 3
1 3 [ -1 -3 5 ] 3 6 7 -3 5
1 3 -1 [ -3 5 3 ] 6 7 -3 5
1 3 -1 -3 [ 5 3 6 ] 7 3 6
1 3 -1 -3 5 [ 3 6 7 ] 3 7
你的任务是找出窗口在各位置时的max value, min value.
输入描述 Input Description
第1行n,k,第2行为长度为n的数组
输出描述 Output Description
2行
第1行每个位置的min value
第2行每个位置的max value
样例输入 Sample Input
8 3
1 3 -1 -3 5 3 6 7
样例输出 Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
数据范围及提示 Data Size & Hint
数据范围:20%: n<=500; 50%: n<=100000;100%: n<=1000000;
program df;
var i,j,n,m,z,l,r,l1,r1:longint;
a,b1,c1,b2,c2,x,y:array[0..3000000] of longint;
begin
readln(n,m);
for i:=1 to n do
read(a[i]);
l:=1; r:=0;
l1:=1; r1:=0;
for i:=1 to m do //先把前m个元素放入
begin
while (l<=r) and (b1[r]<=a[i]) do dec(r);
inc(r);
b1[r]:=a[i];
c1[r]:=i;
while (l1<=r1) and (b2[r1]>=a[i]) do dec(r1);
inc(r1);
b2[r1]:=a[i];
c2[r1]:=i;
end;
x[m]:=b1[l];
y[m]:=b2[l1];
for i:=m+1 to n do
begin
while (l<=r) and (b1[r]<=a[i]) do dec(r); //保证栈递增
inc(r);
b1[r]:=a[i];
c1[r]:=i;
while (l<=r) and (c1[l]<=i-m) do inc(l); //保证单调栈的区间长度等于M
while (l1<=r1) and (b2[r1]>=a[i]) do dec(r1); //保证栈递减
inc(r1);
b2[r1]:=a[i];
c2[r1]:=i;
while (l1<=r1) and (c2[l1]<=i-m) do inc(l1); //和上面一样
x[i]:=b1[l];
y[i]:=b2[l1];
end;
for i:=m to n do
write(y[i],’ ‘);
writeln;
for i:=m to n do
write(x[i],’ ‘);
end.
相关文章推荐
- Codevs 4373 窗口(线段树 单调队列 st表)
- wikioi-1748 瑰丽华尔兹 -单调队列优化DP
- 滑动窗口——单调队列问题
- POJ[2823]窗口 单调队列
- luogu1886:滑动的窗口((线段树+输出优化)|| 单调队列)
- cogs 495 窗口 单调队列第一题
- HDU2870 最大窗口面积(单调队列优化)
- 窗口(单调队列)
- bzoj 1999: [Noip2007]Core树网的核【树的直径+单调队列】
- poj 2823 Sliding Window 单调队列
- hdu-6319-单调队列
- ZOJ_3790_Consecutive Blocks(离散化+二分/单调队列)
- hdu 1506 Largest Rectangle in a Histogram DP 单调队列
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest Problem J 【二分+DP+单调队列】
- BZOJ 2096 单调队列
- 关于单调队列的个人理解
- 【BZOJ1999】树网的核,求树的直径+单调队列乱搞
- [SYSU 1800][BNUOJ 27411] Sequence [单调队列][RMQ]
- POJ 1821 Fence ★(单调队列优化DP)
- 【BZOJ】【P1758】【Wc2010】【重建计划】【题解】【点分治+二分+单调队列】