正文
增加 42 行删除 42 行
123456789101112131415161725 行未改动4344454647484950515238 行未改动9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591605 行未改动1661671681691701711721731742 行未改动1771781791801811821831841851861871881891901911922 行未改动1951961971981992002012022032047 行未改动2122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552563 行未改动2602612622632642652662672682692702712722732742752762772 行未改动2802812822832842852862872884 行未改动2932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363375 行未改动## P5788 【模板】单调栈**思路**模板题**代码**```c++#include <bits/stdc++.h>using namespace std;stack<int> st;int a[3000005],ans[3000005];int main(){// freopen(".in","r",stdin);// freopen(".out","w",stdout);ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int n;25 行未改动return 0;}```## U514895 ١١(❛ᴗ❛)【单调栈】-左侧第一个较小值(不重复)**思路**求每个位置 i 左侧第一个比 $a_i$小的元素下标。从左往右遍历,维护一个单调递增栈,栈中存下标。对于位置 i,弹出所有栈顶 >$a_i$的元素——它们不可能成为后续位置的"左侧较小值"。弹出后栈顶就是左边最近且比$a_i$小的元素下标;栈空则输出 −1。最后将i 入栈。当 n>1000 时,为避免输出过多数据导致卡常,改为输出从第2个位置开始所有答案的异或值。**代码**```c++#include <bits/stdc++.h>using namespace std;38 行未改动return 0;}```## P1901 发射站**思路**这道题的题意就是每个发射站 $i$ 向左/右发射能量,能量被**最近的、比它高的**发射站接收。| 情况 | 栈顶元素 vs 当前元素 | 含义 ||------|---------------------|------|| 栈顶更矮 | `height[st.top()] < height[i]` | 当前站是栈顶的「右侧最近更高站」,栈顶的能量被当前站接收,栈顶出栈 || 栈顶更高 | `height[top] > height_i` | 当前站是栈顶的「左侧最近更高站」,当前站的能量被栈顶接收 |**代码**```c++#include <bits/stdc++.h>using namespace std;#define int long longstack<int> s;struct node{int id;int h;int v;int w;} a[1000005];signed main(){int n;cin >> n;for (int i = 1; i <= n; i++){cin >> a[i].h >> a[i].v;a[i].id = i;}for (int i = 1; i <= n; i++){//被弹出的站他们向右发射的能量全部被当前站 i 接收while (!s.empty() && a[s.top()].h < a[i].h){a[i].w += a[s.top()].v;// 当前站累加被弹栈站的能量s.pop();// 弹出}//如果弹栈后栈非空,说明栈顶比当前站更高//当前站向左发射的能量被这个栈顶接收if (!s.empty()){a[s.top()].w += a[i].v;// 栈顶累加当前站的能量}//入栈s.push(i);}int maxn = 0;for (int i = 1; i <= n; i++){maxn = max(maxn, a[i].w);}cout << maxn << endl;return 0;}```## P8082 [COCI 2011/2012 #4] KEKS**思路**这道题在 $N$ 位数中删除 $K$ 位,要使剩余数最大,就是保留尽可能大的高位数字。- 用单调栈遍历数字,从高位到低位依次入栈。- 如果当前数字比栈顶大,且还能删除,就弹出栈顶(删除一个较小的高位),腾出空间让更大的数字占据更高位置。- 最终栈中留下的就是最优解。若删除次数没用完,从末尾删除即可(保证递减)。- 最后要倒序输出。**代码**```c++5 行未改动{int n, k;cin >> n >> k;cin >> s;for (int i = 0; i < n; i++){//栈顶是较小的高位数字,删掉它让更大的 s[i] 占据更高位while (k > 0 && !st.empty() && st.top() < s[i]){2 行未改动}st.push(s[i]); // 当前数字入栈}// 此时栈内为递减序列,末尾都是最小的,直接删掉即可while (k > 0){st.pop();k--;}//栈转字符串string ans = "";while (!st.empty()){ans += st.top();st.pop();}2 行未改动return 0;}```## P1823 [COI 2007] Patrik 音乐会的等待**思路**每对能互相看见的人,满足:较矮的人向两侧看,被**最近的更高/等高者**阻挡。用单调栈模拟这个过程:- 弹栈时,被弹出的每个人都与当前人配对 -> 他们之间无更高者阻挡- 等高时,累加出现次数7 行未改动int h;int v;// 当前栈中该高度的连续出现次数} a[500005];stack<node> st;signed main(){int n;cin >> n;for (int i = 1; i <= n; i++){cin >> a[i].h;a[i].v = 1;// 初始化}int ans = 0;// 能互相看见的对数for (int i = 1; i <= n; i++){//弹出所有比当前矮的while (!st.empty() && a[i].h > st.top().h){ans += st.top().v; // 栈顶所有人都与当前人配对st.pop();}//栈顶与当前等高if (!st.empty() && a[i].h == st.top().h){node temp = st.top();ans += st.top().v;// 先加上等高元素内部的配对st.pop();// 弹出// 弹出等高者后,如果栈非空,说明存在一个比当前一个更高的阻挡者// 当前人能与这个更高者配对if (!st.empty()){ans++;}// 入栈st.push(node{temp.h, temp.v + 1});}// 栈顶比当前高else{if (!st.empty()){ans++; // 与栈顶配对(+1)}st.push(a[i]);// 入栈3 行未改动return 0;}```## P2422 良好的感觉**思路**对于每个位置 i,我们假设它是区间 [L, R] 中的最小值。那么,为了使舒适度 (min × sum) 最大,我们要找到:1. 左边第一个比 $a_i$ 小的位置 ->记为 $left_i$2. 右边第一个比 $a_i$ 小的位置 ->记为 $right_i$这样,以 $a_i$ 为最小值的最大扩展区间就是 ($left_i$, $right_i$),即 [$left_i$+1, $right_i$-1]。然后计算该区间的和 sum = prefix[$right_i$-1] - prefix[$left_i$],舒适度 = $a_i$ * sum。遍历所有 i,取最大值即可。**代码**```c++#include <bits/stdc++.h>using namespace std;2 行未改动int n, a[N];int b[N], c[N];int s[N];stack<int> st;signed main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);4 行未改动s[i] = s[i - 1] + a[i]; // 前缀和}//求 b[i](左边第一个更小的元素位置)st.push(n);for (int i = n - 1; i >= 1; i--){// 弹栈while (!st.empty() && a[st.top()] > a[i]){b[st.top()] = i; // st.top() 的左边界是 ist.pop();}st.push(i);//入栈}// 左边没有更小的,左边界设为 0while (!st.empty()){b[st.top()] = 0;st.pop();}// 求 c[i](右边第一个更小的元素位置)st.push(1);for (int i = 2; i <= n; i++){// 弹栈while (!st.empty() && a[st.top()] > a[i]){c[st.top()] = i; // st.top() 的右边界是 ist.pop();}st.push(i);}//右边没有更小的,右边界设为 n+1while (!st.empty()){c[st.top()] = n + 1;st.pop();}//枚举每个位置作为最小值int ans = 0;for (int i = 1; i <= n; i++){// 以 a[i] 为最小值的最大区间是 [b[i] + 1, c[i] - 1]int sum = s[c[i] - 1] - s[b[i]];ans = max(ans, sum * a[i]);5 行未改动123456789101112131415161725 行未改动4344454647484950515238 行未改动9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591605 行未改动1661671681691701711721731742 行未改动1771781791801811821831841851861871881891901911922 行未改动1951961971981992002012022032047 行未改动2122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552563 行未改动2602612622632642652662672682692702712722732742752762772 行未改动2802812822832842852862872884 行未改动2932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363375 行未改动## P5788 【模板】单调栈**思路**模板题**代码**```c++#include <bits/stdc++.h>using namespace std;stack<int> st;int a[3000005],ans[3000005];int main(){// freopen(".in","r",stdin);// freopen(".out","w",stdout);ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int n;25 行未改动return 0;}```## U514895 ١١(❛ᴗ❛)【单调栈】-左侧第一个较小值(不重复)**思路**求每个位置 i 左侧第一个比 $a_i$小的元素下标。从左往右遍历,维护一个单调递增栈,栈中存下标。对于位置 i,弹出所有栈顶 >$a_i$的元素——它们不可能成为后续位置的"左侧较小值"。弹出后栈顶就是左边最近且比$a_i$小的元素下标;栈空则输出 −1。最后将i 入栈。当 n>1000 时,为避免输出过多数据导致卡常,改为输出从第2个位置开始所有答案的异或值。**代码**```c++#include <bits/stdc++.h>using namespace std;38 行未改动return 0;}```## P1901 发射站**思路**这道题的题意就是每个发射站 $i$ 向左/右发射能量,能量被**最近的、比它高的**发射站接收。| 情况 | 栈顶元素 vs 当前元素 | 含义 ||------|---------------------|------|| 栈顶更矮 | `height[st.top()] < height[i]` | 当前站是栈顶的「右侧最近更高站」,栈顶的能量被当前站接收,栈顶出栈 || 栈顶更高 | `height[top] > height_i` | 当前站是栈顶的「左侧最近更高站」,当前站的能量被栈顶接收 |**代码**```c++#include <bits/stdc++.h>using namespace std;#define int long longstack<int> s;struct node{int id;int h;int v;int w;} a[1000005];signed main(){int n;cin >> n;for (int i = 1; i <= n; i++){cin >> a[i].h >> a[i].v;a[i].id = i;}for (int i = 1; i <= n; i++){//被弹出的站他们向右发射的能量全部被当前站 i 接收while (!s.empty() && a[s.top()].h < a[i].h){a[i].w += a[s.top()].v;// 当前站累加被弹栈站的能量s.pop();// 弹出}//如果弹栈后栈非空,说明栈顶比当前站更高//当前站向左发射的能量被这个栈顶接收if (!s.empty()){a[s.top()].w += a[i].v;// 栈顶累加当前站的能量}//入栈s.push(i);}int maxn = 0;for (int i = 1; i <= n; i++){maxn = max(maxn, a[i].w);}cout << maxn << endl;return 0;}```## P8082 [COCI 2011/2012 #4] KEKS**思路**这道题在 $N$ 位数中删除 $K$ 位,要使剩余数最大,就是保留尽可能大的高位数字。- 用单调栈遍历数字,从高位到低位依次入栈。- 如果当前数字比栈顶大,且还能删除,就弹出栈顶(删除一个较小的高位),腾出空间让更大的数字占据更高位置。- 最终栈中留下的就是最优解。若删除次数没用完,从末尾删除即可(保证递减)。- 最后要倒序输出。**代码**```c++5 行未改动{int n, k;cin >> n >> k;cin >> s;for (int i = 0; i < n; i++){//栈顶是较小的高位数字,删掉它让更大的 s[i] 占据更高位while (k > 0 && !st.empty() && st.top() < s[i]){2 行未改动}st.push(s[i]); // 当前数字入栈}// 此时栈内为递减序列,末尾都是最小的,直接删掉即可while (k > 0){st.pop();k--;}//栈转字符串string ans = "";while (!st.empty()){ans += st.top();st.pop();}2 行未改动return 0;}```## P1823 [COI 2007] Patrik 音乐会的等待**思路**每对能互相看见的人,满足:较矮的人向两侧看,被**最近的更高/等高者**阻挡。用单调栈模拟这个过程:- 弹栈时,被弹出的每个人都与当前人配对 -> 他们之间无更高者阻挡- 等高时,累加出现次数7 行未改动int h;int v;// 当前栈中该高度的连续出现次数} a[500005];stack<node> st;signed main(){int n;cin >> n;for (int i = 1; i <= n; i++){cin >> a[i].h;a[i].v = 1;// 初始化}int ans = 0;// 能互相看见的对数for (int i = 1; i <= n; i++){//弹出所有比当前矮的while (!st.empty() && a[i].h > st.top().h){ans += st.top().v; // 栈顶所有人都与当前人配对st.pop();}//栈顶与当前等高if (!st.empty() && a[i].h == st.top().h){node temp = st.top();ans += st.top().v;// 先加上等高元素内部的配对st.pop();// 弹出// 弹出等高者后,如果栈非空,说明存在一个比当前一个更高的阻挡者// 当前人能与这个更高者配对if (!st.empty()){ans++;}// 入栈st.push(node{temp.h, temp.v + 1});}// 栈顶比当前高else{if (!st.empty()){ans++; // 与栈顶配对(+1)}st.push(a[i]);// 入栈3 行未改动return 0;}```## P2422 良好的感觉**思路**对于每个位置 i,我们假设它是区间 [L, R] 中的最小值。那么,为了使舒适度 (min × sum) 最大,我们要找到:1. 左边第一个比 $a_i$ 小的位置 ->记为 $left_i$2. 右边第一个比 $a_i$ 小的位置 ->记为 $right_i$这样,以 $a_i$ 为最小值的最大扩展区间就是 ($left_i$, $right_i$),即 [$left_i$+1, $right_i$-1]。然后计算该区间的和 sum = prefix[$right_i$-1] - prefix[$left_i$],舒适度 = $a_i$ * sum。遍历所有 i,取最大值即可。**代码**```c++#include <bits/stdc++.h>using namespace std;2 行未改动int n, a[N];int b[N], c[N];int s[N];stack<int> st;signed main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);4 行未改动s[i] = s[i - 1] + a[i]; // 前缀和}//求 b[i](左边第一个更小的元素位置)st.push(n);for (int i = n - 1; i >= 1; i--){// 弹栈while (!st.empty() && a[st.top()] > a[i]){b[st.top()] = i; // st.top() 的左边界是 ist.pop();}st.push(i);//入栈}// 左边没有更小的,左边界设为 0while (!st.empty()){b[st.top()] = 0;st.pop();}// 求 c[i](右边第一个更小的元素位置)st.push(1);for (int i = 2; i <= n; i++){// 弹栈while (!st.empty() && a[st.top()] > a[i]){c[st.top()] = i; // st.top() 的右边界是 ist.pop();}st.push(i);}//右边没有更小的,右边界设为 n+1while (!st.empty()){c[st.top()] = n + 1;st.pop();}//枚举每个位置作为最小值int ans = 0;for (int i = 1; i <= n; i++){// 以 a[i] 为最小值的最大区间是 [b[i] + 1, c[i] - 1]int sum = s[c[i] - 1] - s[b[i]];ans = max(ans, sum * a[i]);5 行未改动