【洛谷】P5788 【模板】单调栈

题目地址:

https://www.luogu.com.cn/problem/P5788

题目描述:
给出项数为 n n n的整数数列 a 1 … n a_{1 \dots n} a1n。定义函数 f ( i ) f(i) f(i)代表数列中第 i i i个元素之后第一个大于 a i a_i ai的元素的下标,即 f ( i ) = min ⁡ i < j ≤ n , a j > a i { j } f(i)=\min_{i<j\leq n, a_j > a_i} \{j\} f(i)=mini<jn,aj>ai{j}。若不存在,则 f ( i ) = 0 f(i)=0 f(i)=0。试求出 f ( 1 … n ) f(1\dots n) f(1n)

输入格式:
第一行一个正整数 n n n。第二行 n n n个正整数 a 1 … n a_{1\dots n} a1n

输出格式:
一行 n n n个整数 f ( 1 … n ) f(1\dots n) f(1n)的值。

数据范围:
对于 30 % 30\% 30%的数据, n ≤ 100 n≤100 n100
对于 60 % 60\% 60%的数据, n ≤ 5 × 1 0 3 n\leq 5 \times 10^3 n5×103
对于 100 % 100\% 100%的数据, 1 ≤ n ≤ 3 × 1 0 6 1 \le n\leq 3\times 10^6 1n3×106 1 ≤ a i ≤ 1 0 9 1\leq a_i\leq 10^9 1ai109

开一个严格单调上升栈,如果栈非空且栈顶所指数字小于了新来的数,则找到了其右边第一个比其大的数的下标。代码如下:

#include <iostream>
using namespace std;

const int N = 3e6 + 10;
int n;
int stk[N], top;
int a[N], res[N];

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    for (int i = 1; i <= n; i++) {
        while(top && a[stk[top - 1]] < a[i]) {
            res[stk[top - 1]] = i;
            top--;
        }

        stk[top++] = i;
    }

    for (int i = 1; i <= n; i++) cout << res[i] << ' ';
    cout << endl;

    return 0;
}

时空复杂度 O ( n ) O(n) O(n)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值