poj 2823 线段树 求固定区间的最大最小值
2010-04-20 21:02
483 查看
Sliding Window
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.
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
Sample Output
Source
POJ Monthly--2006.04.28, Ikki
也可以使用优先队列的方式
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 10791 | Accepted: 2934 | |
Case Time Limit: 5000MS |
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 |
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
#include <iostream> #include <string> using namespace std; class cNode { public:int l,r; int root; int min,max; }Node[5000001]; int data[1000011]; void build(int l,int r,int root) { Node[root].l = l; Node[root].r = r; if(l == r) { Node[root].min=Node[root].max=data[l]; return; } int mid=(l+r)>>1; build(l,mid,root*2); build(mid+1,r,root*2+1); Node[root].min = min(Node[root*2].min,Node[root*2+1].min); Node[root].max = max(Node[root*2].max,Node[root*2+1].max); } int minl[1000011],maxl[1000011]; void query(int l,int r,int& min,int& max,int root) { if(Node[root].l == l && Node[root].r == r) { min = Node[root].min; max = Node[root].max; return ; } int mid = (Node[root].l + Node[root].r)>>1; if(mid >= r) query(l,r,min,max,root*2); else if(mid<l) query(l,r,min,max,root*2+1); else { int max2,min2; query(l,mid,min,max,root*2); query(mid+1,r,min2,max2,root*2+1); min=min<min2?min:min2; max=max>max2?max:max2; } } int main() { int k,len; while(scanf("%d%d",&k,&len)!=EOF) { for(int i=1;i<=k;i++) scanf("%d",data+i); build(1,k,1); int maxt,mint; for(int i=1;i<=k-len+1;i++) { query(i,i+len-1,mint,maxt,1); minl[i]=mint; maxl[i]=maxt; } for(int i=1;i<=k-len+1;i++) printf("%d ",minl[i]); cout<<endl; for(int i=1;i<=k-len+1;i++) printf("%d ",maxl[i]); cout<<endl; } }
也可以使用优先队列的方式
#include <queue> #include <iostream> using namespace std; int a[1000003]; int outMin[1000003]; int outMax[1000003]; struct cmp{ bool operator() (const int l,const int r) { return a[l]>a[r]; } }; struct cmp2{ bool operator() (const int l,const int r) { return a[l]<a[r]; } }; priority_queue<int,vector<int>,cmp>f1; priority_queue<int,vector<int>,cmp2>f2; int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",a+i); for(int i=1;i<=k;i++) { f1.push(i); f2.push(i); } int cnt1=0; int cnt2=0; outMin[cnt1++]=a[f1.top()]; outMax[cnt2++]=a[f2.top()]; for(int i=k+1;i<=n;i++) { f1.push(i); f2.push(i); while(i-f1.top()>=k) f1.pop(); outMin[cnt1++]=a[f1.top()]; while(i-f2.top()>=k) f2.pop(); outMax[cnt2++]=a[f2.top()]; } for(int i=0;i<=n-k;i++) printf("%d ",outMin[i]); cout<<endl; for(int i=0;i<=n-k;i++) printf("%d ",outMax[i]); cout<<endl; } }
相关文章推荐
- poj 2823 Sliding Window (线段树 求固定区间的最大最小值 )
- poj-2823 -- Sliding Window 区间最大值最小值 RMQ 与线段树
- poj 3264 线段树 求区间最大最小值
- Balanced Lineup POJ - 3264(线段树寻找区间最大值最小值)
- poj3264——Balanced Lineup(线段树+区间最大值与最小值)
- 【POJ 3264】【RMQ 或者线段树】Balanced Lineup 【查询区间内最大最小值的差】
- poj 3264 线段树区间最大最小值
- poj 2823 单调队列 (求特定长度区间的最大最小值)
- poj 2823 poj 3264 线段树维护最大最小值
- POJ 3264 Balanced Lineup(线段树求区间最大最小值)
- nyoj 119 士兵杀敌(三)【线段树区间最大值最小值差】
- POJ 题目2823 Sliding Window(RMQ,固定区间长度)
- POJ 3264 Balanced Lineup 区间查询(两棵树求最大最小值)
- 线段树求解区间最大最小值
- Sliding Window(单调队列维护或线段树求区间最大最小值)
- POJ 2823 线段树区间查询
- NYOJ 1185 最大最小值(线段树—区间求最值)
- HDU 4302 线段树单点更新,维护区间最大最小值
- poj 1750Potted Flower(线段树 区间合并 动态规划 区间求最大连续和)
- poj 1754 I Hate It --线段树(单点更新,区间求最大值)