跳到正文

个人记录

7-31 单调栈和单调队列总结

tanruiqing约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 15 分钟

版本

  1. v1当前

前言

我发誓下一次再也不不注意数据范围了!!!

为什么?

没为什么。。

请移步至 T3 的【数据范围】一处。。

得分

别说了。。

6+100+0+100+10+0+24+18+06+100+0+100+10+0+24+18+0

T1

思路:

因为题目说要求位置 ii 左边和右边的第一个数值小于 aia_i 的数的下标,没有输出 −1-1,因此我们可以想到枚举法。但是 n≤5×105n\le 5\times 10^5,时间复杂度直接炸了。

所以,我们需要优化。

显然,枚举法的内层循环可以优化掉。

用什么优化呢?

观察题目,题目说要找到每一个位置 ii 左侧小于 aia_i 的第一个元素的下标以及每一个位置 ii 右侧小于 aia_i 的第一个元素的下标,可以想到用二分或单调栈实现。

首先考虑二分,二分什么?二分每一个位置 ii 左侧小于 aia_i 的第一个元素的下标以及每一个位置 ii 右侧小于 aia_i 的第一个元素的下标。但是二分的前提条件是需要保证二分序列的单调性,但是题目给出的序列并没有具备单调性,所以二分 out。

其次考虑单调栈,因为需要找到第一个小于的数,所以我们需要维护一个单调递增的序列。因为是小于的数,所以我们要判栈顶是否大于目前数字,大于说明不满足条件,弹出栈。直到第一个小于目前数字的数。需不需要判等于?~~这是一个值得思考的点。~~因为题目说会有重复的数字,所以我们找到了栈顶第一个等于目前数字的数也需要弹出。

答案如何统计?

我们可以用两个数组 l,rl,r,表示答案,在我们上面弹出的操作做完之后,我们就可以记录答案。

注意

在统计答案的时候,需要判断一下栈是否为空,如果为空并且你调用了 st.top(),你就可以获得 RE。

其次,答案数组需要在最前面全部赋值为 −1-1,顺便解决最后输出 −1-1 的情况。

然后我们可以写出这样一段代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int n;
int a[500005];
int l[500005],r[500005];

int read(){
	int cnt=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=!f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		cnt=cnt*10+c-'0';
		c=getchar();
	}
	return ((f==1)?-cnt:cnt);
}

struct node{
	int a[500005],l;
	int top(){
		return a[l];
	}
	void pop(){
		l--;
	}
	void push(int x){
		a[++l]=x;
	}
	int size(){
		return l;
	}
	bool empty(){
		return l==0;
	}
}st;

signed main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0)->sync_with_stdio(false);
	n=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	for(int i=1;i<=n;i++)l[i]=-1,r[i]=-1;
	for(int i=1;i<=n;i++){
		while(!st.empty()&&a[st.top()]>=a[i])st.pop();
		if(!st.empty())r[i]=st.top();
		st.push(i);
	}
	for(int i=n;i>=1;i--){
		while(!st.empty()&&a[st.top()]>=a[i])st.pop();
		if(!st.empty())l[i]=st.top();
		st.push(i);
	}
	for(int i=1;i<=n;i++){
		cout<<r[i]<<" "<<l[i]<<'\n';
	}
	return 0;
}

然后你可以获得 66 分的好成绩。

因为我们跑了两边统计答案的循环,但是用是同一个栈,所以……

打开这里

多测不清空,爆零两行泪……

AC 代码:

不知为什么,这题快读被卡了。。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int n;
int a[500005];
int l[500005],r[500005];

int read(){
	int cnt=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=!f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		cnt=cnt*10+c-'0';
		c=getchar();
	}
	return ((f==1)?-cnt:cnt);
}

struct node{
	int a[500005],l;
	int top(){
		return a[l];
	}
	void pop(){
		l--;
	}
	void push(int x){
		a[++l]=x;
	}
	int size(){
		return l;
	}
	bool empty(){
		return l==0;
	}
}st;

signed main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0)->sync_with_stdio(false);
//	n=read();
	cin>>n;
	for(int i=1;i<=n;i++){
//		a[i]=read();
		cin>>a[i];
	}
	for(int i=1;i<=n;i++)l[i]=-1,r[i]=-1;
	for(int i=1;i<=n;i++){
		while(!st.empty()&&a[st.top()]>=a[i])st.pop();
		if(!st.empty())r[i]=st.top();
		st.push(i);
	}
	while(!st.empty())st.pop();
	for(int i=n;i>=1;i--){
		while(!st.empty()&&a[st.top()]>=a[i])st.pop();
		if(!st.empty())l[i]=st.top();
		st.push(i);
	}
	for(int i=1;i<=n;i++){
		cout<<r[i]<<" "<<l[i]<<'\n';
	}
	return 0;
}

T3

思路:

我们看到“区间”和“最大/小值”这两个词就要想起一个东西——单调队列(滑动窗口),我们可以每次框出一个长度为 kk 的窗口,然后再经过一番操作后得出答案。在这一题我们维护一个单调递减的序列,每一次先将前面超过区间范围的数给弹掉,然后在将后面小于等于目前的数的数字弹掉。最后剩下的就是答案。

  • 后缀最大值的定义:对于一个数 aia_i,在选定的区间 [L∼R][L\sim R] 之内每一个 aj(L≤i<j≤R)a_j(L\le i<j\le R) 都有 aj<aia_j<a_i。

通过定义,我们可以得出如果这个数和队首的元素的数字一样,也需要弹出。

AC 代码

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;

int read(){
	int cnt=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=!f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		cnt=cnt*10+c-'0';
		c=getchar();
	}
	return ((f==1)?-cnt:cnt);
}

struct node{
	int a[1000005],l;
	int top(){
		return a[l];
	}
	void pop(){
		l--;
	}
	void push(int x){
		a[++l]=x;
	}
	int size(){
		return l;
	}
	bool empty(){
		return l==0;
	}
}st;

deque<int> q;

int n,m;

int a[1000005];

signed main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0)->sync_with_stdio(false);
	n=read();
	m=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	for(int i=1;i<=n;i++){
		while(!q.empty()&&q.front()+m<=i)q.pop_front();
		while(!q.empty()&&a[i]>=a[q.back()])q.pop_back();
        //死因:在判 a[i]>=a[q.back()] 时写成了 >。
		q.push_back(i);
		if(i>=m){
			cout<<q.size()<<'\n';
		}
	}
	return 0;
}

T5

思路:

首先:这道题的宽度没有用。

为什么?

因为对于同一高度的建筑,我们不可能将它中间分开用两个不同的区间覆盖(由此我们可以推出:如果有两栋高度一样的楼,我们可以将其合并)。

其次,我们可以观察出:如果有一栋楼高为 88,另一栋楼高为 1010,此时我们一定不会用一个高为 1∼71\sim 7 的区间覆盖而是用一个高度为 88 的区间来进行覆盖。

然后,我们可以维护一个单调递增的栈,在栈顶元素大于目前元素时弹出。如果弹出完栈顶元素和目前元素一样,那么统计答案。

当然,在栈为空的时候也需要累加答案。因为我们需要一张海报来覆盖后面的。

AC 代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read(){
	int cnt=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=!f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		cnt=cnt*10+c-'0';
		c=getchar();
	}
	return ((f==1)?-cnt:cnt);
}

struct node{
	int a[500005],l;
	int top(){
		return a[l];
	}
	void pop(){
		l--;
	}
	void push(int x){
		a[++l]=x;
	}
	int size(){
		return l;
	}
	bool empty(){
		return l==0;
	}
}st;

deque<int> q;

int n;
int x,a[500005];

signed main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0)->sync_with_stdio(false);
	n=read();
	for(int i=1;i<=n;i++){
		x=read();a[i]=read();
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		while(!st.empty()&&a[st.top()]>a[i]){
			st.pop();
		}
		if(st.empty())ans++;
		else if(a[i]!=a[st.top()])ans++;
		st.push(i);
	}
	cout<<ans;
	return 0;
}

T6

思路:

我们可以维护一个队列,每次将一个珠子加入队列,如果我们已经拥有了 mm 颗珠子,那么我们就缩小队列长度,更新答案。

死因:push 珠子的时候外面多加了一层 while,在输入时两个变量放反了导致枚举的是位置。

AC 代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read(){
	int cnt=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=!f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		cnt=cnt*10+c-'0';
		c=getchar();
	}
	return ((f==1)?-cnt:cnt);
}

deque<int> q;

struct node{
	int pos,id;
};

vector<node> e;

int n,m;

int vis[1000005];

int cnt;

int maxx;

bool cmp(node a,node b){
	return a.pos<b.pos;
}

signed main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0)->sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int t;
		cin>>t;
		while(t--){
			int x;
			cin>>x;
			e.push_back(node{x,i});
		}
	}
	int minn=1e18;
	sort(e.begin(),e.end(),cmp);
	for(int i=0;i<n;i++){
		q.push_back(i);
		int id=e[i].id;
		if(vis[id]==0){
			cnt++;
		}
		vis[id]++;
		while(!q.empty()&&cnt>=m){
			int j=q.front();
			q.pop_front();
			int id=e[j].id;
			vis[id]--;
			if(vis[id]==0){
				cnt--;
				minn=min(minn,e[i].pos-e[j].pos);
			}
		}
	}
	cout<<minn;
	return 0;
}

T7

死因:栈用成队列,100->28。

思路:

我们可以用一个 struct 记录每个身高为 aia_i 的人数。

然后,我们维护一个单调递减的栈,然后将比目前这个人身高矮的人 pop 掉,但是此时他们可以互相看见,所以需要累加答案。

当然,如果弹掉的那个人和目前的人的升高一样的话,我们就需要将这个身高的人数增加。

最后,如果栈的元素个数不为 00,那么那么栈顶元素和目前元素可以互相看到,所以答案累加。

最后加入当前元素即可。

AC 代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read(){
	int cnt=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=!f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		cnt=cnt*10+c-'0';
		c=getchar();
	}
	return ((f==1)?-cnt:cnt);
}

int n;
int a[500005];

struct node{
	int x,y;
};

stack<node> q;

signed main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0)->sync_with_stdio(false);
	n=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		node b=node{a[i],1};
		while(!q.empty()&&q.top().x<=a[i]){
			ans+=q.top().y;
			if(q.top().x==a[i]){
				b.y+=q.top().y;
			}
			q.pop();
		}
		if(!q.empty())ans++;
		q.push(b);
	}
	cout<<ans;
	return 0;
}

> EndEnd

原文