Codeforces 982 B. Bus of Characters(模拟一个栈)

解题思路:

  排序之后模拟一个栈(也可以用真的栈),时间复杂度o(n)。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct node{
    int val;int idx;
}w[200010];
bool cmp(node x, node y){
    return x.val < y.val;
}

char a[400010];
stack <node> s;
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1;i <= n; i++) scanf("%d", &w[i].val),w[i].idx = i;
    sort(w+1, w+1+n, cmp);
    scanf("%s", a+1);
    node l;
    int j = 1;
    for(int i = 1;i <= 2*n; i++){
        if(a[i] == '0'){
            s.push(w[j++]);
            printf("%d ",w[j-1].idx);
        }else{
            l = s.top();
            printf("%d ",l.idx);
            s.pop();
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhangjiuding/p/9057420.html