单调栈就是内部元素具有单调性的栈。
单调栈-左侧第一个较小值(不重复)
模板题,我们可以模拟一下过程:
数组为 5 0 3 100
首先读入 5,现在栈里面首先没有任何元素,答案就是 -1,并将 5 的下标入栈,为什么是下标而不是数字本身呢,因为根据下标可以查询到数字,根据数字却查不到下标。
读入 0,但是栈顶元素所对应的数字比 0 大,所以答案也是 -1,由于栈内的元素是有序的,所以不需要关注栈内其它元素所对应的数字与当前数字的大小关系,这一步中也要将 0 的下标入栈。
读入 3,栈顶元素所对应的数字比 3 小,弹出,这是栈顶元素所对应的数字比 3 大停止弹出,答案就是栈顶元素,但是有一点需要注意,我们不仅仅只要关注栈顶第一个元素所对应的数字与当前元素的大小关系,还得在出栈后判断那时的栈顶元素所对应的数字与当前元素的大小,所以这一步不可以用 if,而是得用 while。如果在某一步弹出过程中把栈变空了,那么就说明在这个数字之前没有比它大的了,答案也是 -1。
读入 100,出现了上一步所说的情况,把栈弹空了,输出 -1。
在完成上面的步骤之后,有一种特殊的情况,最后还有一些元素没有从栈内弹出,这些元素就没有答案,为了避免这种情况,可以在数组中放一个极小值的哨兵哨兵来避免这种情况。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[500005];
int st[500005];
int ans[500005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[n+1]=-1e9;
int top=0;
for(int i=1;i<=n+1;i++){
while(top>0&&a[st[top]]>a[i]){
top--;
}
if(top==0){
ans[i]=-1;
}else{
ans[i]=st[top];
}
st[++top]=i;
}
if(n>1000){
int cnt=0;
for(int i=2;i<=n;i++){
cnt^=ans[i];
}
cout<<cnt;
return 0;
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
return 0;
}单调栈-右侧第一个较小值(不重复)
与上一道题类似,只不过答案并不是栈顶元素。
一个数被其它元素弹出,这个被弹出的数右侧第一个比自己小的数就是把自己弹出的那个数。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[500005];
int st[500005];
int ans[500005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[n+1]=-1e9;
int top=0;
for(int i=1;i<=n+1;i++){
while(top>0&&a[st[top]]>a[i]){
if(i==n+1){
ans[st[top]]=-1;
}else{
ans[st[top]]=i;
}
top--;
}
st[++top]=i;
}
if(n>1000){
int cnt=0;
for(int i=2;i<=n;i++){
cnt^=ans[i];
}
cout<<cnt;
return 0;
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
return 0;
}单调栈-左右两侧第一小于值(存在重复情况)
如果按照前两题的方法去做会发现左侧的一个小于的值有错误。
以样例为例子,到第四个时,出现错误,原因是左侧的 3 使得单调栈不能继续出栈得到正确答案,这样说得到的答案就是左侧第一个小于等于的值,为了避免这种情况,可以把判断条件改成 a[st[top]]>=a[i]。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[500005];
int st[500005];
int ans_l[500005];
int ans_r[500005] ;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[0]=1e9+1;
a[n+1]=-1;
int top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>=a[i]){
top--;
}
if(top==0){
ans_l[i]=-1;
}else{
ans_l[i]=st[top];
}
st[++top]=i;
}
top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>a[i]){
if(i==n+1){
ans_r[st[top]]=-1;
}else{
ans_r[st[top]]=i;
}
top--;
}
st[++top]=i;
}
for(int i=1;i<=n;i++){
cout<<ans_l[i]<<" "<<ans_r[i]<<"\n";
}
return 0;
}单调栈-左右两侧第一小于等于值(存在重复情况)
与上道题一样,一个是 >,另一个是 >=。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[500005];
int st[500005];
int ans_l[500005];
int ans_r[500005] ;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[0]=1e9+1;
a[n+1]=-1;
int top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>a[i]){
top--;
}
if(top==0){
ans_l[i]=-1;
}else{
ans_l[i]=st[top];
}
st[++top]=i;
}
top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>=a[i]){
if(i==n+1){
ans_r[st[top]]=-1;
}else{
ans_r[st[top]]=i;
}
top--;
}
st[++top]=i;
}
for(int i=1;i<=n;i++){
cout<<ans_l[i]<<" "<<ans_r[i]<<"\n";
}
return 0;
}ღꦿ࿐