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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1001080
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
int A[maxn],ans1[maxn],ans2[maxn];
struct ST
{
int l,r,mink,maxk;
}st[maxn<<2];
inline int min(int a,int b)
{
return a>b?b:a;
}
inline int max(int a,int b)
{
return a>b?a:b;
}
void PushUp(int id)
{
st[id].maxk = max(st[id<<1].maxk,st[id<<1|1].maxk);
st[id].mink = min(st[id<<1].mink,st[id<<1|1].mink);
}
void buildtree(int id,int l,int r)
{
st[id].l = l,st[id].r = r;
if(l == r)
{
st[id].maxk = st[id].mink = A[l];
return;
}
int mid = (l+r)>>1;
buildtree(lson);
buildtree(rson);
PushUp(id);
}
int query(int id,int l,int r,int ope)
{
if(st[id].l == l && st[id].r == r)
{
if(ope == 1) return st[id].maxk;
else return st[id].mink;
}
if(st[id<<1].r >= r)
{
return query(id<<1,l,r,ope);
}
if(st[id<<1|1].l <= l)
{
return query(id<<1|1,l,r,ope);
}
if(ope == 1)
{
return max(query(id<<1,l,st[id<<1].r,ope),query(id<<1|1,st[id<<1|1].l,r,ope));
}
return min(query(id<<1,l,st[id<<1].r,ope),query(id<<1|1,st[id<<1|1].l,r,ope));
}
int main()
{
//freopen("in.txt","r",stdin);
int n,k;
while(scanf("%d%d",&n,&k)==2)
{
for(int i = 1;i <= n;i++)
{
scanf("%d",&A[i]);
}
buildtree(1,1,n);
for(int i = 1;i <= n-k+1;i++)
{
ans1[i] = query(1,i,i+k-1,1);
ans2[i] = query(1,i,i+k-1,2);
}
for(int i = 1;i <= n-k+1;i++)
{
printf("%d",ans2[i]);
if(i == n-k+1)printf("\n");
else printf(" ");
}
for(int i = 1;i <= n-k+1;i++)
{
printf("%d",ans1[i]);
if(i == n-k+1)printf("\n");
else printf(" ");
}
}
return 0;
}
稍后更新单调队列做法。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
每次从队尾删除比t小的或等于的,然后推入t.然后取队首。判断队首的下标是否符合要求
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 1110080
struct Num
{
int id,key;
};
Num que[maxn],quem[maxn];
int ans[maxn],ansm[maxn];
bool cmp(Num a,Num b)
{
return a.key > b.key;
}
bool cmp1(Num a,Num b)
{
return a.key < b.key;
}
int main()
{
//freopen("in.txt","r",stdin);
int n,k;
while(scanf("%d%d",&n,&k)==2)
{
int top = 0,rear = 0;
int topm = 0, rearm = 0;
if(k == 1)
{
for(int i = 1;i <= n;i++)
{
scanf("%d",&ans[i]);
ansm[i] = ans[i];
}
goto L;
}
for(int i = 1;i < k;i++)
{
int key; scanf("%d",&key);
Num num;
num.id = i;num.key = key;
que[rear++] = num;
quem[rearm++] = num;
}
sort(que,que+rear,cmp);
sort(quem,quem+rear,cmp1);
for(int i = k;i <= n;i++)
{
int key; scanf("%d",&key);
Num num;
num.id = i;num.key = key;
while(rear > top)
{
if(que[rear-1].key <= key) rear--;
else break;
}
que[rear++] = num;
while(i-que[top].id+1 > k) top++;
ans[i] = que[top].key;
while(rearm > topm)
{
if(quem[rearm-1].key >= key) rearm--;
else break;
}
quem[rearm++] = num;
while(i-quem[topm].id+1 > k) topm++;
ansm[i] = quem[topm].key;
}
L:
for(int i = k;i <= n;i++)
{
printf("%d",ansm[i]);
if(i == n) printf("\n");
else printf(" ");
}
for(int i = k;i <= n;i++)
{
printf("%d",ans[i]);
if(i == n) printf("\n");
else printf(" ");
}
}
return 0;
}