poj 2823
来源:互联网 发布:mac放大显示 快捷键 编辑:程序博客网 时间:2024/06/08 05:13
Description
An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position Minimum value Maximum 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
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.
Output
There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.
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
Source
POJ Monthly–2006.04.28, Ikki
分析:
这个题是单调队列的入门题。
求最大值:建立一个单调递减队列,元素从左到右依次入队,入队之前必须从队列尾部开始删除那些比当前入队元素小或者相等的元素,直到遇到一个比当前入队元素大的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。
求最小值:建立一个单调递增队列,元素从左到右依次入队,入队之前必须从队列发问开始删除那些比当前入队元素大或者相等的元素,直到遇到一个比当前入队元素小的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。
设窗口大小为k,本题解法为建立两个单调队列,先从原数组中取前k个元素按上述要求入队,然后获取队头元素,再把下一个元素放入队中,再获取头元素,这样一直到最后一个元素为止。最后把答案从头到尾依次输出即可。
代码:
var ans:array[0..1000000,1..2] of longint; a,w:array[0..1000000] of longint; h,f,r,mid,m,n,i,v:longint;begin readln(n,m); for i:=1 to n do read(a[i]); a[0]:=-2147483647; for i:=1 to n do begin if a[i]>a[w[h]] then begin h:=h+1; w[h]:=i; end else begin f:=1; r:=h; repeat mid:=(f+r) div 2; if (a[w[mid]]>=a[i]) and (a[w[mid-1]]<a[i]) then break; if (a[w[mid]]>=a[i]) then r:=mid-1 else f:=mid+1; until f>r; h:=mid; w[h]:=i; end; if i>=m then begin f:=1; r:=h; v:=0; repeat mid:=(f+r) div 2; if w[mid]<i-m+1 then f:=mid+1; if w[mid]>=i-m+1 then begin v:=mid; r:=mid-1; end; until f>r; ans[i-m+1,1]:=a[w[v]]; end; end; a[0]:=2147483647; h:=0; fillchar(w,sizeof(w),0); for i:=1 to n do begin if a[i]<a[w[h]] then begin h:=h+1; w[h]:=i; end else begin f:=1; r:=h; repeat mid:=(f+r) div 2; if (a[w[mid]]<=a[i]) and (a[w[mid-1]]>a[i]) then break; if (a[w[mid]]<=a[i]) then r:=mid-1 else f:=mid+1; until f>r; h:=mid; w[h]:=i; end; if i>=m then begin f:=1; r:=h; v:=0; repeat mid:=(f+r) div 2; if w[mid]<i-m+1 then f:=mid+1; if w[mid]>=i-m+1 then begin v:=mid; r:=mid-1; end; until f>r; ans[i-m+1,2]:=a[w[v]]; end; end; for i:=1 to n-m+1 do write(ans[i,1],' '); writeln; for i:=1 to n-m+1 do write(ans[i,2],' '); writeln;end.
- poj 2823
- POJ 2823
- poj 2823
- POJ 2823
- poj 2823
- POj 2823
- POJ 2823
- POJ 2823
- poj-2823
- POJ 2823
- poj 2823
- poj 2823
- poj 2823
- 【POJ】2823
- POJ 2823 Sliding Window
- poj 2823 Sliding Window
- Poj 2823 (单调队列)
- poj 2823【单调队列】
- Audroid-studio2.2安装1%与windows不兼容和Error occurred during initialization of VM 的问题
- 跨域
- 关于Web_reg_save_param的一个BUG说明
- 状态栏的图标变成黑色
- android studio 配置androidannotations注入框架
- poj 2823
- 设置Liferay网站不用写:8080就可以访问
- 几种常用的特征选择方法
- 微信开发专题---5菜单的定义
- Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架
- 简单说说java的3大方向!
- Java中的23种设计模式
- 90. Subsets II
- i++与++i的区别