正文
增加 185 行删除 5 行
73 行未改动7475767778798081828384858687882 行未改动91929394959697989916 行未改动11611711811912012112212312452 行未改动17717817918018118218318423 行未改动20820921021121221321421521619 行未改动23623723823973 行未改动$$\LARGE\bold{例题}$$## [$\color{black}\bold{B3614\ 【模板】栈}$](/problem/B3614)### 思路如题目描述。:::info[代码]```cpp line-numbers#include<iostream>#define int unsigned long long //不开 ull 见祖宗using namespace std;const int N=1e6+010;int st[N],tot;int n;2 行未改动cin.tie(0),cout.tie(0);int t;cin>>t;while(t--){tot=0; //多测清空cin>>n;string str;while(n--){cin>>str;16 行未改动return 0;}```:::## [$\color{black}\bold{P1449\ 中缀表达式}$](/problem/P1449)### 思路定义两个栈,输入数则存入数栈,输入符号则去除数栈最上方两个元素运算后再存回去。:::info[代码]```cpp52 行未改动return 0;}```:::$$\Huge \bold {单调栈}$$23 行未改动while(!st.empty()&&st.top()<x)st.pop();st.push(x);```## [$\color{black}\bold{P5788\ 【模板】单调栈}$](/problem/P5788)### 思路对于每个在插入第 $i$ 个元素时弹出的元素,记录其结果为 $i$ 。:::info[代码]```cpp line-numbers19 行未改动return 0;}```:::73 行未改动7475767778798081828384858687882 行未改动91929394959697989916 行未改动11611711811912012112212312452 行未改动17717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629723 行未改动32132232332432532632732832933033133219 行未改动35235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841973 行未改动$$\LARGE\bold{例题}$$## [$\color{black}\bold{B3614\ 【模板】栈}$](https://www.luogu.com.cn/problem/B3614)### 思路如题目描述。:::info[代码]```cpp line-numbers#include<iostream>#define int unsigned long long //不开 ull 见祖宗using namespace std;const int N=1e6+010;int st[N],tot;int n;2 行未改动cin.tie(0),cout.tie(0);int t;cin>>t;while(t--){tot=0; //多测清空cin>>n;string str;while(n--){cin>>str;16 行未改动return 0;}```:::## [$\color{black}\bold{P1449\ 后缀表达式}$](https://www.luogu.com.cn/problem/P1449)### 思路定义两个栈,输入数则存入数栈,输入符号则去除数栈最上方两个元素运算后再存回去。:::info[代码]```cpp52 行未改动return 0;}```:::## [$\color{black}\bold{P1175\ 表达式的转换}$](https://www.luogu.com.cn/problem/P1175)### 思路**输入:**+ 对于数,直接插入到用来存后缀表达式链表最后;+ 对于 $($ ,入符号栈,然后不管;+ 对于除 $)$ 和 ^ 外的其他运算符,将运算级 $\leq$ 它的插入链表,然后将其入符号栈;+ 对于 ^ 与上一条几乎相同,但把 $\leq$ 改为 $<$ ;+ 对于 $)$ ,将它与它对应的 $($ 之间的所有运算符插入链表。**计算:**+ 从链表开头开始遍历,遇到操作符便将其与上两个数运算并删除,接着在同位置插入运算结果,然后输出,到无运算符(遍历到最后)为止。:::info[代码]```cpp line-numbers#include<iostream>#include<map>using namespace std;const int N=1e2+010;struct Node{int x;bool isnum;}s[N];char st[N];int tot,tp;map<char,int> mp;struct List{Node x;int lst,nxt;}str[N];int h=1,t=0;void In(){char ch=getchar(),lst='+';int x=0;while(ch^-1){if('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^48);else{if(isdigit(lst)){s[++tot]={x,1};x=0;}if(ch=='(') st[++tp]=ch;else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'){while(tp&&st[tp]!='('&&(mp[st[tp]]>mp[ch]||ch^'^'&&mp[st[tp]]==mp[ch]))s[++tot]={st[tp--],0};st[++tp]=ch;}else if(ch==')'){while(st[tp]!='(')s[++tot]={st[tp--],0};tp--;}}lst=ch;ch=getchar();}while(tp) s[++tot]={st[tp--],0};}void Change(){for(int i=1;i<=tot;i++)str[++t]={s[i],t-1,t+1};str[1].lst=-1;str[t].nxt=-1;}int qpow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a,b>>=1;}return ans;}int main(){mp['+']=mp['-']=1;mp['*']=mp['/']=2;mp['^']=3;mp['(']=mp[')']=4;In();Change();for(int i=h;~i;i=str[i].nxt){if(str[i].x.isnum)cout<<str[i].x.x<<' ';else cout<<(char)str[i].x.x<<' ';}cout<<'\n';for(int i=h;i!=-1;i=str[i].nxt){if(!str[i].x.isnum){char ch=str[i].x.x;int x=str[str[str[i].lst].lst].x.x,y=str[str[i].lst].x.x;if(ch=='+')str[str[str[i].lst].lst].x.x=x+y;if(ch=='-')str[str[str[i].lst].lst].x.x=x-y;if(ch=='*')str[str[str[i].lst].lst].x.x=x*y;if(ch=='/')str[str[str[i].lst].lst].x.x=x/y;if(ch=='^')str[str[str[i].lst].lst].x.x=qpow(x,y);str[str[str[i].lst].lst].nxt=str[i].nxt;str[str[i].nxt].lst=str[str[i].lst].lst;for(int j=h;~j;j=str[j].nxt){if(str[j].x.isnum)cout<<str[j].x.x<<' ';else cout<<(char)str[j].x.x<<' ';}cout<<'\n';}}return 0;}```:::$$\Huge \bold {单调栈}$$23 行未改动while(!st.empty()&&st.top()<x)st.pop();st.push(x);```$$\LARGE\bold{例题}$$## [$\color{black}\bold{P5788\ 【模板】单调栈}$](https://www.luogu.com.cn/problem/P5788)### 思路对于每个在插入第 $i$ 个元素时弹出的元素,记录其结果为 $i$ 。:::info[代码]```cpp line-numbers19 行未改动return 0;}```:::## [$\color{black}\bold{P1823\ [COI\ 2007]\ Patrik\ 音乐会的等待}$](https://www.luogu.com.cn/problem/P1823)### 思路维护一个 $\operatorname{pair<int,int>}$ 的单调栈, $\operatorname{first}$ 存高度, $\operatorname{second}$ 存此高度人的个数(题目没说身高相等的不算一对),在弹出时让答案加上被弹出高度的人的个数,弹出完毕,判断要插入元素与顶部元素大小关系(记得判空):若比顶部元素高度小,答案加一再插入;若与顶部元素高度相等,加上顶部高度人数$+1$(若只有一种高度(即栈中只有一个元素)则不$+1$)再让顶部高度人数$+1$;否则直接插入。:::info[代码]```cpp#include<iostream>#include<cstdio>#include<stack>#define int long long //不开 long long 见祖宗using namespace std;const int maxn=5e5+010;template<typename T>struct Stack{int tp;T st[maxn];inline T &operator [](const int x){return st[x];}inline bool empty()const{return !tp;}inline void clear(){tp=0;}inline int size(){return tp;}inline void push(const T x){st[++tp]=x;}inline bool pop(){if(!tp) return 0;tp--;return 1;}inline T top(){return st[tp];}};Stack<pair<int,int> > st;int n,ans;signed main(){cin>>n;for(int i=1,x;i<=n;i++){cin>>x;while(!st.empty()&&st.top().first<x){ans+=st.top().second;st.pop();}if(st.top().first>x){ans++;st.push({x,1});}else if(st.top().first==x){ans+=st.top().second+(bool)(st.size()^1);st[st.size()].second++;}else st.push({x,1});}cout<<ans;return 0;}```:::