单调栈简单题目整理

这篇博客探讨了如何使用栈解决矩阵类问题,包括寻找直方图中的最大矩阵和城市游戏中的最大正方形。算法通过维护单调栈来高效地计算最大面积,对于接雨水问题,同样利用单调栈来计算柱子间的雨水体积。这些题目展示了栈在处理最高或最低位置问题上的应用。
摘要由CSDN通过智能技术生成

最大矩阵类问题

1.求最大矩阵

题目链接

题目:直方图中的最大矩阵

#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)a.size()
#define db1(x) cout<<#x<<" = "<<x<<endl
#define db2(x, y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define endl "\n"
//#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6+10;
int n;
ll h[N];
int w[N], p[N];
void sol() {
    for(int i = 1;i <= n; ++i) scanf("%lld", &h[i]);
    h[n+1] = 0;
    int c = 0;
    ll ma = 0;
    for(int i = 1;i <= n+1; ++i) {
        ll x = h[i];
        if(h[i]>=p[c]) {
            p[++c] = h[i];
            w[c] = 1;
        }
        else {
            int width = 0;
            while(h[i]<p[c]) {
                width += w[c];
                ma = max(ma, 1ll*width*p[c--]);
            }
            p[++c] = h[i]; w[c] = width+1;
        }
    }
    printf("%lld\n", ma);
}
int main()
{
    while(scanf("%d", &n), n)
    sol();
    return 0;
}
2 求最大正方形,也就是最大矩阵稍微改改

题目链接

题目:城市游戏

需要先预处理从当前位置向右边连续的F的个数O(n^2)
然后枚举起始列,对每一列进行一个单调栈处理出最大正方形O(n^2)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)a.size()
#define db1(x) cout<<#x<<" = "<<x<<endl
#define db2(x, y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define endl "\n"
#define inf 0x3f3f3f3f
#define MEM(a, b) memset(a, b, sizeof(a))
//#define int long long
using namespace std;
typedef long long ll;
typedef pair<string, int> pii;
typedef pair<int, int> piii;
typedef unsigned long long ull;
const int N = 2010, maxn = 1e6+10;
const ll mod = 1e9+7;

int n, m, k;
char s[N][N];
int num[N][N];

int calc(int x)
{
    int h[N], w[N], p[N], c = 0;
    for(int i = 1; i <= n; ++i) h[i] = num[i][x], w[i] = p[i] = 0;
    h[0] = 0;
    h[n+1] = 0;
    p[0] = 0;

    int ans = 0;
    for(int i = 1;i <= n+1; ++i) {
        if(h[i] >= p[c]) p[++c] = h[i], w[c] = 1;
        else {
            int width = 0;
            while(h[i] < p[c]) {
                width += w[c];
                ans = max(ans, width*p[c--]);
            }
            p[++c] = h[i], w[c] = width+1;
        }
    }
    return ans;
}
void sol() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
    for(int j = 1; j <= m; ++j) scanf(" %c", &s[i][j]);

    for(int i = 1;i <= n; ++i)
    for(int j = 1;j <= m; ++j)
    {
        if(s[i][j] == 'F') num[i][j] = 1;
        else continue;
        if(num[i][j-1]>1&&s[i][j]==s[i][j-1]) {
            num[i][j] = num[i][j-1]-1;
        }
        else {
            int k = j;
            while(k+1 <= m&&s[i][j]==s[i][k+1]) k++, num[i][j]++;
        }
    }
    int ans = 0;
    for(int st = 1; st <= m; ++st)
    ans = max(ans, calc(st));
    printf("%lld\n", 3ll*ans);
}
signed main()
{
    sol();
}

最近的最高或最低的位置问题。

1 模板类

题目链接

题目:仰视奶牛

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)a.size()
#define db1(x) cout<<#x<<" = "<<x<<endl
#define db2(x, y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define endl "\n"
#define inf 0x3f3f3f3f
#define MEM(a, b) memset(a, b, sizeof(a))
//#define int long long
using namespace std;
typedef long long ll;
typedef pair<string, int> pii;
typedef pair<int, int> piii;
typedef unsigned long long ull;
const int N = 2e5+10, maxn = 1e6+10;
const ll mod = 1e9+7;
int n, m, k;
int h[N], p[N], ans[N];
void sol() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &h[i]);
    int c = 0;
    for(int i = 1; i <= n; ++i)
    {
        if(c==0||h[i]<=h[p[c]]) p[++c] = i;
        else {
            while(c!=0&&h[i]>h[p[c]]) {
                ans[p[c--]] = i;
            }
            p[++c] = i;
        }
    }
    for(int i = 1;i <= n; ++i)
    printf("%d\n", ans[i]);
}
signed main()
{
    sol();
}
2 接雨水类

题目链接

题目:接雨水

维护一个单调严格递减的栈,若当前柱子高于栈顶的柱子,退栈直到符合单调性;否则加入栈顶。每次退栈时,累加雨水的体积(每次算一层)

#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)a.size()
#define db1(x) cout<<#x<<" = "<<x<<endl
#define db2(x, y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define endl "\n"
#define inf 0x3f3f3f3f
#define MEM(a, b) memset(a, b, sizeof(a))
//#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef unsigned long long ull;
const int N = 2e5+10, maxn = 1e6+10;
const ll mod = 1e9+7;

int n, m, k;
int h[N];
void sol() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &h[i]);
    stack<int> st;
    ll ans = 0;
    for(int i = 1;i <= n; ++i)
    {
        while(!st.empty()&&h[st.top()]<h[i]) {
            int t = st.top(); st.pop();
            if(!st.empty()){
                int l = i-st.top()-1;
                int w = min(h[i], h[st.top()])-h[t];
                ans += l*w;
            }
        }
        st.push(i);
    }
    printf("%lld\n", ans);
}
signed main()
{
    sol();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
单调栈是一种常用的数据结构,用于解决一类特定的问题,其中最常见的问题是找到数组中每个元素的下一个更大或更小的元素。在Codeforces编程竞赛中,单调栈经常被用于解决一些与数组相关的问题。 下面是单调栈的一般思路: 1. 创建一个空栈。 2. 从左到右遍历数组元素。 3. 对于每个元素,将其与栈顶元素进行比较。 - 如果当前元素小于等于栈顶元素,则将当前元素入栈。 - 如果当前元素大于栈顶元素,则将栈顶元素弹出,并将当前元素入栈。 4. 重复步骤3,直到遍历完所有元素。 这样,最后剩下的栈中元素就是没有下一个更大或更小元素的元素。在使用单调栈求解具体问题时,我们可以根据需要进行一些特定的操作。 例如,如果要找到一个数组中每个元素的下一个更大的元素,可以使用单调递减栈。具体操作如下: 1. 创建一个空栈和一个空结果数组。 2. 从左到右遍历数组元素。 3. 对于每个元素,将其与栈顶元素进行比较。 - 如果当前元素小于等于栈顶元素,则将当前元素入栈。 - 如果当前元素大于栈顶元素,则将栈顶元素弹出,并将其在结果数组中的位置记录为当前元素的下一个更大元素的索引。 4. 将当前元素入栈。 5. 重复步骤3和4,直到遍历完所有元素。 6. 结果数组中没有下一个更大元素的位置,可以设置为-1。 以下是一个使用单调递减栈求解下一个更大元素问题的示例代码: ```cpp #include <iostream> #include <stack> #include <vector> std::vector<int> nextGreaterElement(std::vector<int>& nums) { int n = nums.size(); std::vector<int> result(n, -1); std::stack<int> stack; for (int i = 0; i < n; i++) { while (!stack.empty() && nums[i] > nums[stack.top()]) { result[stack.top()] = i; stack.pop(); } stack.push(i); } return result; } int main() { std::vector<int> nums = {1,3, 2, 4, 5, 1}; std::vector<int> result = nextGreaterElement(nums); for (int i = 0; i < result.size(); i++) { std::cout << "Next greater element for " << nums[i] << ": "; if (result[i] != -1) { std::cout << nums[result[i]]; } else { std::cout << "None"; } std::cout << std::endl; } return 0; } ``` 以上代码将输出: ``` Next greater element for 1: 3 Next greater element for 3: 4 Next greater element for 2: 4 Next greater element for 4: 5 Next greater element for 5: None Next greater element for 1: None ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值