牛客OI赛制测试赛2 C题

链接:https://www.nowcoder.com/acm/contest/185/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述
给出一个数列 A,求出一个数列B.
其中Bi 表示 数列A中 Ai 右边第一个比 Ai 大的数的下标(从1开始计数),没有找到这一个下标 Bi 就为0
输出数列B

输入描述:
第一行1个数字 n (n ≤ 10000)
第二行n个数字第 i 个数字为 Ai (0 ≤ Ai ≤ 1000000000)

输出描述:
一共一行,第 i 个数和第 i+1 个数中间用空格隔开.
示例1
输入
6
3 2 6 1 1 2

输出
3 3 0 6 6 0

/*
栈模拟
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3
using namespace std;
typedef long long LL;
const int maxn=1e4+5;

stack<int>sta;
int a[maxn],b[maxn];
int N;
int main()
{
    scanf("%d",&N);
    while(!sta.empty())sta.pop();
    for(int i=1;i<=N;i++)
    {
        scanf("%d",&a[i]);
        while(!sta.empty()&&a[i]>a[sta.top()])
        {
            b[sta.top()]=i;
            sta.pop();
        }
        sta.push(i);
    }
    for(int i=1;i<N;i++)printf("%d ",b[i]);
    printf("%d\n",b[N]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41646772/article/details/82495289