Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 59304 | Accepted: 17027 | |
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
题目大意:
给出一个数列,求每一个长度为k区间的最大值、最小值
解题思路:
先只考虑求最大值:
显然,当区间变化的时候,最大值可能也会改变。如果我们能够维护一个数组,求出当前位置后面的最大值,那么问题会简单许多。
单调队列,维护一个队列d,和l(头指针)、r(尾指针),d[i]表示d[i-1]位置后的最大值(随着区间变化不断更新)。每当一个新的数入队时,从后向前找到比它大的第一个数,删除后面的数列,插入该数。出队时直接l++。
先预处理出第一个区间的d数组(最左),当区间右移时(相当于把一个数加入d数组)入队,当区间覆盖过最大值时出队。
最小值同理。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define LL long long
const int N=1000010;
const int MAX=1e9+7;
int n,a[N],m;
int d[N],l,r;
inline void R(int &v)//读入、输出优化
{
v=0;
char ch=getchar();
int f=0;
while(!isdigit(ch)){if(ch=='-')f=1;ch=getchar();}
while(isdigit(ch)){v=(v<<3)+(v<<1)+ch-'0';ch=getchar();}
if(f) v=-v;
}
namespace ib {char b[100];}
inline void P(int x)
{
if(x==0) {putchar(48); return;}
if(x<0) {putchar('-'); x=-x;}
char *s=ib::b;
while(x) *(++s)=x%10, x/=10;
while(s!=ib::b) putchar((*(s--))+48);
}
void push(int x)//入队操作
{
int i,j;
for(i=r;i>=l;i--) if(x<=d[i]) break;//二分可能更快(?)
r=i;
d[++r]=x;
}
void init()
{
int i,j;
l=1,r=1;
memset(d,0,sizeof(d));
int maxx=-MAX,b;
for(i=1;i<=m;++i)
if(a[i]>=maxx) maxx=a[i],b=i;//保留相同的数避免出队时删除错误
d[r]=maxx;
for(i=b+1;i<=m;++i) push(a[i]);
}
void shan()//出队操作
{
l++;
}
void _push(int x)//博主只会Ctrl+C所以有两遍(哈?)
{
int i,j;
for(i=r;i>=l;i--) if(x>=d[i]) break;
r=i;
d[++r]=x;
}
void _init()
{
int i,j;
l=1,r=1;
memset(d,0,sizeof(d));
int minn=MAX,b;
for(i=1;i<=m;++i)
if(a[i]<=minn) minn=a[i],b=i;
d[r]=minn;
for(i=b+1;i<=m;++i) _push(a[i]);
}
void _shan()
{
l++;
}
int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
int i,j;
while(scanf("%d",&n)!=EOF)
{
scanf("%d",&m);
for(i=1;i<=n;i++)
R(a[i]);
_init();
P(d[l]);putchar(' ');
for(i=2;i<=n-m+1;++i)
{
if(a[i-1]==d[l])_shan();
_push(a[i+m-1]);;
P(d[l]);putchar(' ');
}
printf("\n");
init();
P(d[l]);putchar(' ');
for(i=2;i<=n-m+1;++i)
{
if(a[i-1]==d[l])shan();
push(a[i+m-1]);
P(d[l]);putchar(' ');
}
}
return 0;
}
结语:
*单调队列
典型的队列思想