T1 P5788 【模板】单调栈
这道题是个模板,要求离它最近比它大的,用暴力肯定是过不了,我们定义一个栈,栈里面存的是下标,而里面存的下标都代表这个下标还没找到比他大的才会在里面,我们每次把一个新元素入栈时比较一下,如果栈顶比他小,栈顶的就要等于i了,不停的这样,直到栈里面没有元素,或比当前的大才入栈,就是要保持这个栈时从小到大的。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,s;
vector<pair<int,int> >vec[100005];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
bool ok[100050];
int dist[100005];
void dij(){
fill(dist,dist+n+1,1e18);
dist[s]=0;
q.push({0,s});
for(int i=1;i<=n;i++){
while(ok[q.top().second]&&!q.empty())q.pop();
int u=q.top().second;
q.pop();
ok[u]=true;
for(int j=0;j<vec[u].size();j++){
int v=vec[u][j].first,w=vec[u][j].second;
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
q.push({dist[v],v});
}
}
}
}
signed main(){
cin>>n>>m>>s;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
vec[u].push_back({v,w});
}
dij();
for(int i=1;i<=n;i++){
cout<<dist[i]<<' ';
}
return 0;
}T2 U514895 ١١(❛ᴗ❛)【单调栈】-左侧第一个较小值(不重复)
这道题可以使用单调栈算法求解每个元素左侧第一个比它小的元素位置:从右向左遍历数组,维护一个单调递增栈(栈底到栈顶递增),当当前元素a[i]小于栈顶元素时,说明找到了栈顶元素左侧第一个比它小的位置就是i,将结果存入b[st.top()]并弹出栈顶,然后将当前下标i压入栈;如果n≤1000则直接输出所有b[i]值(即每个位置左侧第一个比它小的下标),如果n>1000则计算b[2]到b[n]的异或和并输出。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
typedef long long ll;
int n,a[N],b[N];
stack<int>st;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
fill(b,b+N,-1);
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=n;i>=1;i--){
while(st.size()&&a[st.top()]>a[i]){
b[st.top()]=i;
st.pop();
}
st.push(i);
}
if(n>1000){
ll ans=b[2];
for(int i=3;i<=n;i++)ans^=b[i];
cout<<ans;
}else{
for(int i=1;i<=n;i++)cout<<b[i]<<' ';
}
return 0;
}T3 U514939 ١١(❛ᴗ❛)【单调栈】-左右两侧第一小于等于值(存在重复情况)
这道题我们使用单调栈算法求解每个元素左侧和右侧第一个小于等于它的元素位置(处理重复元素时取等号):第一次从左向右遍历,维护一个单调非递减栈,当当前元素a[i]小于等于栈顶元素时,说明找到了栈顶元素右侧第一个小于等于它的位置就是i,将结果存入R[st.top()]并弹出栈顶,然后压入i;第二次从右向左遍历,同样维护单调栈,当当前元素a[i]小于等于栈顶元素时,说明找到了栈顶元素左侧第一个小于等于它的位置就是i,将结果存入L[st.top()]并弹出栈顶,然后压入i;最终输出每个位置的L[i](左侧第一个小于等于的下标)和R[i](右侧第一个小于等于的下标),若不存在则为-1。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,a[N],L[N],R[N];
stack<int>st;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
fill(L,L+N,-1);
fill(R,R+N,-1);
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>=a[i]){
R[st.top()]=i;
st.pop();
}
st.push(i);
}
while(!st.empty())st.pop();
for(int i=n;i>=1;i--){
while(!st.empty()&&a[st.top()]>=a[i]){
L[st.top()]=i;
st.pop();
}
st.push(i);
}
for(int i=1;i<=n;i++)cout<<L[i]<<' '<<R[i]<<'\n';
return 0;
}T4 P12241 [蓝桥杯 2023 国 C] 最大区
这段代码使用单调栈算法解决最大区间问题:通过两次遍历分别找到每个位置右侧和左侧第一个比它小的元素位置(分别存入r[i]和l[i]),这样以a[i]为最小值的区间就能从l[i]+1延伸到r[i]-1,长度为r[i]-l[i]-1,然后计算a[i]乘以这个长度并取最大值,最终得到所有可能区间中的最大价值。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
typedef long long ll;
int n,a[N],l[N],r[N];
stack<int>st;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>a[i]){
r[st.top()]=i;
st.pop();
}
st.push(i);
}
while(!st.empty()){
r[st.top()]=n+1;
st.pop();
}
for(int i=n;i>=1;i--){
while(!st.empty()&&a[st.top()]>a[i]){
l[st.top()]=i;
st.pop();
}
st.push(i);
}
while(!st.empty()){
l[st.top()]=0;
st.pop();
}
ll ans=0;
for(int i=1;i<=n;i++)ans=max(ans,(ll)a[i]*(r[i]-l[i]-1));
cout<<ans;
return 0;
}T5 P2866 [USACO06NOV] Bad Hair Day S
这道题和奶牛的事业统计很像,只不过奶牛的视野统计要输出每头奶牛能看到的数量,这个不用输出每头奶牛能看见的数量,代码通过单调栈维护一个递减序列,在遍历过程中累加栈的大小(即当前能看到新奶牛的奶牛数量),最终直接输出总和,实现了一次遍历得到答案的高效解法。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=8e4+10;
typedef long long ll;
int n,a[N];
stack<int>st;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
ll ans=0;
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]<=a[i])st.pop();
ans+=st.size();
st.push(i);
}
cout<<ans;
return 0;
}