STL L - Bus of Characters

题目
In the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is wi centimeters. All integers wi are distinct.

Initially the bus is empty. On each of 2n stops one passenger enters the bus. There are two types of passengers:

an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it;
an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it.
You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.

Input
The first line contains a single integer n (1≤n≤200000) — the number of rows in the bus.

The second line contains the sequence of integers w1,w2,…,wn (1≤wi≤109), where wi is the width of each of the seats in the i-th row. It is guaranteed that all wi are distinct.

The third line contains a string of length 2n, consisting of digits ‘0’ and ‘1’ — the description of the order the passengers enter the bus. If the j-th character is ‘0’, then the passenger that enters the bus on the j-th stop is an introvert. If the j-th character is ‘1’, the the passenger that enters the bus on the j-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal n), and for each extrovert there always is a suitable row.

Output
Print 2n integers — the rows the passengers will take. The order of passengers should be the same as in input.

Examples
Input
2
3 1
0011
Output
2 1 1 2
Input
6
10 8 9 11 13 5
010010011101
Output
6 6 2 3 3 1 4 4 1 2 5 5
Note
In the first example the first passenger (introvert) chooses the row 2, because it has the seats with smallest width. The second passenger (introvert) chooses the row 1, because it is the only empty row now. The third passenger (extrovert) chooses the row 1, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 2, because it is the only row with an empty place.
题解
优先队列放入每一排的长度,小的优先。
遍历上车顺序:
如果是内向的人,找到没有人且最窄的位置
将内向人选的位置放在一个集合里面(用栈)
如果是外向的人,在集合里面选一个最宽的位置。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<vector>
#include<set>
#include<cstring>
#include<cstdlib>
#include <time.h>
#include<stack>
const int maxn=2e5+5;
using namespace std;
struct dd{
    int v,id;
    bool friend operator < (dd a,dd b){
    }
};
int main()
{
    int n;cin>>n;
    priority_queue<dd>q;
    for(int i=0;i<n;i++){
        int x;cin>>x;
        q.push({x,i+1});
    }
    string s;cin>>s; 
    stack<dd>st;
    int ans[maxn];
    for(int i=0;i<2*n;i++){
        if(s[i]=='0'){
            ans[i]=q.top().id;
            st.push(q.top());
            q.pop();
        }
        if(s[i]=='1'){
            ans[i]=st.top().id;
            st.pop();
        }
        printf("%d ",ans);
    }
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值