数据结构:单调栈

数据结构:单调栈

题目描述

在这里插入图片描述
输入样例

5
3 4 2 7 5

输出样例

-1 3 -1 2 2

参考代码

#include <iostream>

using namespace std;

const int N = 100010;

int stk[N], top;
int n, x;

int main()
{
    cin >> n;
    while (n--)
    {
        cin >> x;
        while (top && stk[top] > x) top--;
        if (!top) cout << -1 << ' ';
        else cout << stk[top] << ' ';
        stk[++top] = x;
    }
    return 0;
}

相关推荐

  1. 数据结构单调

    2024-06-05 19:35:59       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-05 19:35:59       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-05 19:35:59       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-05 19:35:59       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-05 19:35:59       20 阅读

热门阅读

  1. stm32和esp32硬件资源上有什么区别

    2024-06-05 19:35:59       8 阅读
  2. 微信小程序动画

    2024-06-05 19:35:59       11 阅读
  3. 防止重复调用

    2024-06-05 19:35:59       8 阅读
  4. HFish蜜罐实践:网络安全防御的主动出击

    2024-06-05 19:35:59       9 阅读
  5. python中的正则表达式

    2024-06-05 19:35:59       8 阅读
  6. Linux Python基础教程:从入门到精通的全方位解析

    2024-06-05 19:35:59       8 阅读