Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 56848 | Accepted: 16323 | |
Case Time Limit: 5000MS |
Description
An array of size
n ≤ 10
6 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.
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
题意;就是i从第k个开始,求i之前k个数(包括i)的最大值和最小值。
思路:其实这跟求单调队列一样只是这里多了一个判定必须是前k个的最大小值。
单调递减队列是这么一个队列,它的头元素一直是队列当中的最大值,而且队列中的值是按照递减的顺序排列的。我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素。
代码:
1》 用单调队列:
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) #define inf 0x3f3f3f3f #define ll long long #define N 1000001 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; int k,a[N],n,ans[N],ans1[N]; struct node { int num; int index;//记录下标 }p[N]; void getmin() { int head=1,tail=0; for(int i=1;i<k;i++)//前k-1个数不存在队首下标比i前面k-1个数还要小的情况 { while(head<=tail&&a[i]<p[tail].num) tail--;//如果加入的数比队列前的数小则删除队首元素然后再加入队列 tail++; p[tail].num=a[i]; p[tail].index=i; } for(int i=k;i<=n;i++) { while(head<=tail&&a[i]<p[tail].num) tail--; tail++; p[tail].num=a[i]; p[tail].index=i; while(i-p[head].index>=k) head++;//如果队首元素不在所查询的下标内就删除 ans[i-k]=p[head].num;//将求出的最小值存起来方便之后的输出 } } void getmax()//与前面的求最小值类似 { int head=1,tail=0; for(int i=1;i<k;i++) { while(head<=tail&&p[tail].num<a[i]) tail--; tail++; p[tail].num=a[i]; p[tail].index=i; } for(int i=k;i<=n;i++) { while(head<=tail&&p[tail].num<a[i]) tail--; tail++; p[tail].num=a[i]; p[tail].index=i; while(i-p[head].index>=k) head++; ans1[i-k]=p[head].num; } } int main() { while(~scanf("%d%d",&n,&k)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } getmin();getmax(); for(int i=0;i<=n-k;i++) { printf("%d ",ans[i]); } printf("\n"); for(int i=0;i<=n-k;i++) { printf("%d ",ans1[i]); } printf("\n"); } }
2》 用优先队列
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) #define inf 0x3f3f3f3f #define ll long long #define N 1000001 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; int n,k,maxx[N],minn[N],a[N]; int t=0,t1=0; struct cmp1//定义比较算子 { bool operator() (const int a1,const int a2) { return a[a1]>a[a2];//较大的优先出列 } }; struct cmp2 { bool operator() (const int a1,const int a2) { return a[a1]<a[a2];//较小的优先出列 } }; priority_queue<int,vector<int>, cmp1> q; priority_queue<int,vector<int>,cmp2 > p; void judge() { for(int i=0; i<k; i++)//前k个元素入队 { p.push(i); q.push(i); } minn[t++]=a[q.top()];//最小的出队 maxx[t1++]=a[p.top()];//最大的出队 for(int i=k; i<n; i++) { p.push(i); q.push(i); while(i-q.top()>=k) q.pop();//如果队首元素不在范围内就删除队首元素 while(i-p.top()>=k) p.pop(); minn[t++]=a[q.top()];//最小的出队 maxx[t1++]=a[p.top()];//最大的出队 } } int main() { while(~scanf("%d%d",&n,&k)) { for(int i=0; i<n; i++) { scanf("%d",&a[i]); } judge(); for(int i=0; i<=n-k; i++) printf("%d ",minn[i]); printf("\n"); for(int i=0; i<=n-k; i++) printf("%d ",maxx[i]); printf("\n"); } }