CodeForces - 982B Bus of Characters (栈+队列)

B. Bus of Characters
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.

思路:

这是一个贪心题。第一行输入一个数字n,第二行输入n个数字代表每个座位有几个空座,第三行代表上来的人。如果上来的是0,那么他要坐在没有人的最小的座位上,如果上来的是1,那么他要坐在有0坐过的最大的位置上,并且霸占全部位置。

所以先把座位从小到大排列一下,遇到0就顺序压入队列,并且把这个值再压入栈,遇到1就读取栈的top压入队列。

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define For(a,b) for(int i=0;i<b;i++)
#define mem(x) memset(x,0,sizeof(x))
#define Debug(x) cout<<"x= "<<x<<endl
#define sf scanf
#define pf printf
#define maxn 300010
int gcd(int a,int b){ return b>0?gcd(b,a%b):a;}
typedef long long ll;
using namespace std;
int n,t;
typedef struct Node{
    int w,id;
}node;
node A[maxn];

bool cmp(node a,node b){
    return a.w<b.w;
}
char q;
stack <node> S;
queue <int> Q;
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        sf("%d",&A[i].w);
        A[i].id=i+1;
    }
    sort(A,A+n,cmp);
    t=0;
    for(int i=0;i<2*n;i++){
        cin>>q;
        if(q=='0'){
            S.push(A[t]);
            Q.push(A[t].id);
            t++;
        }
        if(q=='1'){
            node a=S.top();
            Q.push(a.id);
            S.pop();
        }
    }
    while(!Q.empty()){
        cout<<Q.front()<<" ";
        Q.pop();
    }
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37360631/article/details/81320893