单调队列滑动窗口学习笔记poj 2823

poj 2823 单调队列滑动窗口的经典入门题
题目:
Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 79685 Accepted: 22530
Case Time Limit: 5000MS

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.
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


思路:
应该可以st表预处理如何o(1)询问每一次的结果,但是为了学习单调队列我还是用单调队列写了一发,我写的单调队列是用deque实现的滑动窗口。
单调队列的原理并不是很难,只要记住新来的必须加入,之前不合格的淘汰掉即可。
单调队列维护最大值的队列中下标增加值一定是减小的,维护最小值的队列中下标增加值一定是递增的。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<set>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#define endl '\n'
#define css(n) cout<<setiosflags(ios::fixed)<<setprecision(n); 
#define sd(a) scanf("%d",&a)
#define sld(a) scanf("%lld",&a)
#define m(a,b) memset(a,b,sizeof a)
#define pb push_back 
#define lson id<<1
#define rson id<<1|1
#define pi acos(-1)
typedef long long ll;
using namespace std;
const int maxn = 1e6+50,modd = 1e9 + 7,inf = 0x3f3f3f3f,INF = 0x7fffffff;
inline ll min(ll a,ll b){return a < b ? a : b;}
inline ll max(ll a,ll b){return a > b ? a : b;}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d; }
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}//求逆元 
inline ll madd(ll a,ll b){return (a%modd+b%modd)%modd;}//加后取模 
inline ll mmul(ll a,ll b){return a%modd * b%modd;}//乘后取模 
int n,m,t;
typedef pair<int,int> pr;//fir为数值,snd为位置。 
deque<pr> r;
deque<pr> d;
int mx[maxn];
int mi[maxn];
int main()
{
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);std::cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int a;cin>>a;
		while(!r.empty()&&r.back().first<=a) r.pop_back();
		r.push_back(pr(a,i));
		if(i>=m)
		{
			while(!r.empty()&&r.front().second<=i-m) r.pop_front();
			mx[i]=r.front().first;
		}
		while(!d.empty()&&d.back().first>=a) d.pop_back();
		d.push_back(pr(a,i));
		if(i>=m)
		{
			while(!d.empty()&&d.front().second<=i-m) d.pop_front();
			mi[i]=d.front().first;
		}
	}
	for(int i=m;i<=n;i++)
	{
		if(i==m) cout<<mi[i];
		else cout<<" "<<mi[i];
	}
	cout<<endl;
	for(int i=m;i<=n;i++)
	{
		if(i==m) cout<<mx[i];
		else cout<<" "<<mx[i];
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值