题目:
Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 48443 | Accepted: 13988 | |
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
Source
POJ Monthly--2006.04.28, Ikki
题意:给一个长度为n的序列,求出其中每个长度为k的区间的最大值和最小值。
思路:我们可以用两个单调队列来分别维护最大值和最小值,先使前k个元素入队,找到其中的最大值和最小值(即队首元素),然后把后面的元素一次入队,每一个元素入队就代表了一个新的长度为k的区间,然后我们找出跟当前位置的距离在k以内的队首保存下来即可。
代码:
题意:给一个长度为n的序列,求出其中每个长度为k的区间的最大值和最小值。
思路:我们可以用两个单调队列来分别维护最大值和最小值,先使前k个元素入队,找到其中的最大值和最小值(即队首元素),然后把后面的元素一次入队,每一个元素入队就代表了一个新的长度为k的区间,然后我们找出跟当前位置的距离在k以内的队首保存下来即可。
代码:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}
/*#ifdef HOME
freopen("in.txt","r",stdin);
#endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME
int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判断正负
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
struct node
{
int num;
int po;
};
int a[1000000+5];
node q1[1000000+5];
node q2[1000000+5];
int ans1[1000000+5];
int ans2[1000000+5];
int main()
{int n,k;
RII(n,k);
for(int i=0;i<n;i++)
RI(a[i]);
int front1=0,rear1=0,front2=0,rear2=0;
for(int i=0;i<k&&i<n;i++)
{while(front1<rear1&&q1[rear1-1].num>=a[i])
rear1--;
q1[rear1].num=a[i];
q1[rear1++].po=i;
while(front2<rear2&&q2[rear2-1].num<=a[i])
rear2--;
q2[rear2].num=a[i];
q2[rear2++].po=i;
}
int p1=0,p2=0;
ans1[p1++]=q1[front1].num;
ans2[p2++]=q2[front2].num;
for(int i=k;i<n;i++)
{
while(front1<rear1&&q1[rear1-1].num>=a[i])
rear1--;
q1[rear1].num=a[i];
q1[rear1++].po=i;
while(front2<rear2&&q2[rear2-1].num<=a[i])
rear2--;
q2[rear2].num=a[i];
q2[rear2++].po=i;
while(i-q1[front1].po>=k)
front1++;
ans1[p1++]=q1[front1].num;
while(i-q2[front2].po>=k)
front2++;
ans2[p2++]=q2[front2].num;
}
for(int i=0;i<p1-1;i++)
printf("%d ",ans1[i]);
printf("%d\n",ans1[p1-1]);
for(int i=0;i<p2-1;i++)
printf("%d ",ans2[i]);
printf("%d\n",ans2[p2-1]);
return 0;
}