Bus of Character

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≤200 00) — the number of rows in the bus.

The second line contains the sequence of integers w1,w2,…,wn(1≤wi≤10^9), 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.

Example Input

2

3 1

0011

Example Output

2

1

1

2

题意

公交车上有n排座位,每拍都有两个位子,每排座位的宽度都不一样,开始时公交车是空的。有两种类型的乘客,内向乘客和外向乘客:
内向乘客会选择两个位子都为空的座位,并且选择在所有空着的排中,座位长度最小的座位;外向乘客会选择已经有一个座位被占据的一排,并且选择这些排中座位宽度最大的座位。

输入: 第一行输入为座位的排数n,第二行为每排座位的宽度,第三行是一个长度为2n的由’0‘’1‘组成的字符串,’0‘表示乘客为内向,’1‘表示乘客为外向。

输出:乘客选择的座位排数,顺序应与乘客上车的顺序保持一致

分析

数据的存储:
1、因为座位信息包含序号和宽度,所以定义一个结构数组来存储座位信息。

定义一个结构数组存储座位信息

2、需要保存内向乘客的选择结果,在外向乘客进行选择时,倒序输出利用栈来保存内向乘客的选择结果。

用栈保存’0‘乘客的选择结果

数据的处理:
1、内向乘客在选择座位时,时按照座位的宽度,所以要对结构数组进行排序。

  结构数组排序

2、根据乘客属性,依次选择座位
当上车的是内向乘客时,将排序过的结构数组的最小的一个.num压入队列,把.num压入栈,为之后的外向乘客选择座位提供备选,利用栈先进后出的特性,进栈是按照内向乘客选择座位的顺序,宽度逐渐增大,那么出栈的时候,则是最后进栈的,也就是宽度最大的先出。

选择座位

3、输出结果

代码

#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std; 

stack<int> st;//声明一个元素为int 类型的栈 st 
queue<int> q;//声明一个元素为int类型的队列, q 

struct node{
	int width;
	int num;
}no[201000];//结构,用来存储座位的宽度以及序号 

bool cmp(node a,node b){
	return a.width<b.width;
}//cmp为sort(t,t+n,cmp)中小于的判定标准 

int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>no[i].width;
		no[i].num=i;
	}//输入椅子的宽度,并为椅子赋序号 
	string s;
	cin>>s;//输入乘客属性 
	sort(no+1,no+1+n,cmp);//将椅子按照宽度,进行从小到大的排序 
	int x=1;//因为string是从s[0]开始的,而no[]是从下标为1开始的,所以需要x=1; 
	for(int i=0;i<s.size();i++){
		if(s[i]=='0'){ //如果输入为0 
			q.push(no[x].num);//将排序过的结构数组的最小的一个.num压入队列,
			 
			st.push(no[x].num);//并把.num压入栈,为之后输入为 1 做准备 
			x++;// x计数的是no[],与i的值无关,对应0乘客,需要没有人的座位,从保存座位的struct中赋值 

		}
		else{ //输入为1 
			int b=st.top(); //取栈顶,应为.pop() 会将栈顶元素清除所以要提前保留 
			st.pop(); //出栈 
			q.push(b);
			
		}
		
	}
	
	while(!q.empty())
	{
		cout<<q.front()<<endl;
		q.pop();
	}
	
	return 0;
}

知识点

  1. c++ sort函数用法
  2. 队列、栈
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值