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 nn rows of seat, each having 22 seats. The width of both seats in the ii-th row is wiwi centimeters. All integers wiwi are distinct.

Initially the bus is empty. On each of 2n2n 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 nn (1n2000001≤n≤200000) — the number of rows in the bus.

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

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

Output

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

Examples
input
Copy
2
3 1
0011
output
Copy
2 1 1 2 
input
Copy
6
10 8 9 11 13 5
010010011101
output
Copy
6 6 2 3 3 1 4 4 1 2 5 5 
Note

In the first example the first passenger (introvert) chooses the row 22, because it has the seats with smallest width. The second passenger (introvert) chooses the row 11, because it is the only empty row now. The third passenger (extrovert) chooses the row 11, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row 22, because it is the only row with an empty place.


题意:在一个汽车上,有n排座位,每排座位有;两个座位,然后2n个车站,每个车站上来一个乘客,0表示内向乘客,1表示外向乘客, 让你们给他们排座位。排座位的要求是:

  • 内向的人总是选择两个座位都是空的一排。在这些行中,他选择了一个座位宽度最小的,并占了其中的一个座位;
  • 外向的人总是选择一排座位,而这个位置正好是一个内向的人所占的座位。在这几排中,他选择了座位宽度最大的一排,并在其中占据了空位。

输入n排,然后输入每排座位的宽度。然后输入2n个乘客的是内向还是外向的,给你每一行的座位宽度和乘客进入公共汽车的顺序。决定每一位乘客将坐哪一排。

题解:贪心      最开始自己写是写炸了的,用了标记,但是座位宽度太大,数组越界。参考网上博客的思路是开栈和队列做的,思路很不错。开结构体里面存每排座位宽度和当座位的排号。按照宽度从小到大排序,遇见0就是存入队列(先进先出),然后在把这个座位的排号存入栈(先进后出),遇见1就出栈,存入队列。最后输出队列,不过CF大佬们都是用的pair这种更简单的方法。还不会用,把两个数据合成一个,然后排序,也用了栈。

栈+队列+结构体:

#include<bits/stdc++.h>
using namespace std;
stack<int>st;
queue<int> q;
struct node
{
    int w,num;
} s[200100];
bool cmp(node a,node b)
{
    return a.w<b.w;
}
int main()
{
    int n;
    string a;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>s[i].w,s[i].num=i;///输入座位宽度,也把座位第几行存入进去
    sort(s+1,s+1+n,cmp);///对输入的宽度从小到大排序
    cin>>a;
    int x=1;
    for(int i=0; i<a.size(); i++)
    {
        if(a[i]=='0')
            q.push(s[x].num),st.push(s[x++].num);
        else
        {
            int b=st.top();
            st.pop();
            q.push(b);
        }
    }
    while(!q.empty())
        cout<<q.front()<<" ",q.pop();
    return 0;
}

pair+栈:

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

int w[200005];
char inp[400005];
pair<int,int> a[200005];
stack<int> s;
int main(){
	int n,i,j,k;
	cin >> n;
	for(i=0;i<n;i++){
		scanf(" %d",w+i);
		a[i]=make_pair(w[i],i+1);
	}
	sort(a,a+n);
	k=0;
	scanf(" %s",inp);
	for(i=0;i<n+n;i++){
		j = inp[i]-'0';
		if(j==0){
			printf("%d ",a[k].second);
			s.push(a[k].second);
			k++;
		}
		else{
			printf("%d ",s.top());
			s.pop();
		}
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值