Codeforces Round #484 (Div. 2) Codeforces982B. 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

传送门:http://codeforces.com/problemset/problem/982/B

题意:输入一个n,有n行座位,数字代表座位的w,座位按顺序从1~n编号,接下来是一个长度为2n的由01构成的字符串,0代表这是一个内向的人,1代表这是一个外向的人。内向的人每次会选择还未被选择的座位里w最小的那一个,外向的人会选择已经被内向的人占领的位置里w最大的那一个。最后输出每个人选择的座位编号。

扫描二维码关注公众号,回复: 1968297 查看本文章

思路:外向人要在前面内向人选择的座位里进行选择,而且和内向人的选择倾向相反,可以想到用栈来解决。先把座位按w升序排序,把内向人选择了的座位放进栈中,每次外向人需要的时候就把栈顶元素取出,那么此时取出的必然是当前已经被内向人选择了的权值最大的那一个。

吐槽:一开始暴力sort,果然tle啦mdzz

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxv = 2*1e5+5;

int n;
string s;
int ans[maxv];
queue<int>q;
stack<int>st;

struct node 
{
	int w,in,out;
	int id;
}a[maxv];

bool cmp(node a,node b)
{
	return a.w<b.w;
}

int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i].w;
		a[i].id=i+1;
		a[i].in=a[i].out=0;
	}
	getchar();
	cin>>s;
	sort(a,a+n,cmp);
	int co=0;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='0')
		{
			q.push(a[co].id);
			st.push(a[co++].id);
		}
		else
		{
			int ss=st.top();
			st.pop();
			q.push(ss);
		}
	}
	while(!q.empty())
	{
		cout<<q.front()<<" ";
		q.pop();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39396954/article/details/80956720