#7~10TLE,求大佬指教!
查看原帖
#7~10TLE,求大佬指教!
1076957
SeaDchen楼主2023/9/3 16:53
#include<iostream>
#include<stack>
using namespace std;

int num[3000005],result[3000005];

int main(){
    stack<int> sta;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>num[i];
    }
    sta.push(1);int i=2;
    while(i<=n){
        while(!sta.empty() && num[sta.top()]<num[i]){
            result[sta.top()]=i;
            sta.pop();
        }
        sta.push(i);
        i++;
    }
    while(!sta.empty()){
        result[sta.top()]=0;
        sta.pop();
    }
    for(int i=1;i<=n;i++){
        if(i!=1){
            cout<<" ";
        }
        cout<<result[i];
    }
    
    return 0;
}

2023/9/3 16:53
加载中...