Bus of Characters(栈和队列)
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.
The first line contains a single integer nn (1≤n≤2000001≤n≤200000) — the number of rows in the bus.
The second line contains the sequence of integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤1091≤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.
Print 2n2n integers — the rows the passengers will take. The order of passengers should be the same as in input.
2
3 1
0011
2 1 1 2
6
10 8 9 11 13 5
010010011101
6 6 2 3 3 1 4 4 1 2 5 5
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.
- 一个内向者总是选择两个座位都没人的一排。 在这些排中,他选择座位宽度最小的,并占据了其中的一个座位;
- 一个外向型的人总是选择有人的一排。 在这些排中,他选择了座位宽度最大的那个,并占据了空位。
Input
Output
打印 2n 个整数 - 乘客将坐的排。 乘客的顺序应与输入的顺序相同。
Sample Input
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
Hint
在第一个例子中,第一个乘客(内向)选择第二排,因为它具有最小宽度的座位。 第二位乘客(内向)选择第1行,因为它现在是唯一的空行。 第三位乘客(外向型)选择第一排,因为它只有一个占用的座位,座位宽度是这些排中最大的。 第四位乘客(外向性)选择第二排,因为它是唯一一个有空位的排。
解题思路:我们知道内向的人会去选择那些空位并且宽度最小的座位,而外向的人却是选择有人的座位边的座位并且尽可能的座位宽度要大,这里其实是可以使用一个栈来模拟的,根据题目所给的信息,我们可以知道,肯定是先有内向的人上车再有外向的人上车,内向人上车就可以看做一个入栈的过程,记录内向人的座号,外向人上车必然会选择之前内向人做过的排位,而恰到好处的是这个题目的要求,内向人尽可能地选择宽度小的座位,而外向人尽可能选择宽度大的座位,在这里就可以现将座位的宽度升序排序,使内向上车的人尽可能得到宽度小的座位,而外向上车的人会得到靠近内向人并且座位宽度尽可能大的座位(栈中的第一个元素)
#include<stdio.h>
#include<algorithm>
#include<stack>
using namespace std;
struct node
{
int val;
int idx;
} a[200010];
char x[400010];
int my_cmp(node a,node b)
{
if(a.val<b.val)
return 1;
else
return 0;
}
stack<node>s;
int main()
{
int n,i,j;
struct node k;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i].val);
a[i].idx=i;
}
sort(a,a+n,my_cmp);
getchar();
gets(x);
j=0;
for(i=0; i<2*n; i++)
{
if(x[i]=='0')
{
s.push(a[j]);
j++;
printf("%d ",a[j-1].idx+1);
}
else
{
k=s.top();
printf("%d ",k.idx+1);
s.pop();
}
}
return 0;
}
Bus of Characters(栈和队列)的更多相关文章
- [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...
- Swift 算法实战之路:栈和队列
这期的内容有点剑走偏锋,我们来讨论一下栈和队列.Swift语言中没有内设的栈和队列,很多扩展库中使用Generic Type来实现栈或是队列.笔者觉得最实用的实现方法是使用数组,本期主要内容有: 栈和 ...
- Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B
原博主:https://blog.csdn.net/amovement/article/details/80358962 B. Bus of Characters time limit per tes ...
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- 剑指Offer面试题:6.用两个栈实现队列
一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...
- C实现栈和队列
这两天再学习了数据结构的栈和队列,思想很简单,可能是学习PHP那会没有直接使用栈和队列,写的太少,所以用具体代码实现的时候出现了各种错误,感觉还是C语言功底不行.栈和队列不论在面试中还是笔试中都很重要 ...
- JavaScript数组模拟栈和队列
*栈和队列:js中没有真正的栈和队列的类型 一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭 FILO 何时使用:今后只要仅希望数组只能从一端进 ...
- 用JS描述的数据结构及算法表示——栈和队列(基础版)
前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
- JavaScript中的算法之美——栈、队列、表
序 最近花了比较多的时间来学习前端的知识,在这个期间也看到了很多的优秀的文章,其中Aaron可能在这个算法方面算是我的启蒙,在此衷心感谢Aaron的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...
随机推荐
- wordpress | 网站访问速度优化方案(Avada)
一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...
- 09.安装Collabora Online服务
安装Collabora Online服务 参考博客:http://blog.sina.com.cn/s/blog_16b158be80102xp5u.html 一.安装docker服务 yum ins ...
- .Net Core如何在任意位置获取配置文件的内容
前几天群里有人问,我想在程序里的任意位置读取appsetting.json里的配置,该怎么搞. 话不多说上源码 首先,要想读取配置文件我们要用到IConfiguration 接口,这个接口在Start ...
- linux系统快速安装宝塔
宝塔面板分linux面板和windows面板,安装宝塔linux面板首先要访问宝塔官网查看对应版本进行选择 宝塔面板的安装需要注意的地方有: 1.纯净系统 2.确保是干净的操作系统,没有安装过其它环境 ...
- Python对文件目录的操作
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()返回指定目录下的所有文件和目录 ...
- Fax Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Que ...
- Django templates加载css/js/image等静态资源
配置步骤: 1.首先在应用下面创建static目录 2.将静态资源拷贝进去 3.在应用的settings.py文件中添加 import os BASE_PATH = os.path.dirname(o ...
- 大数据技术原理与应用——大数据处理架构Hadoop
Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台,为用户提供了系统底层细节透明的分布式基础架构. Hadoop是基于Java语言开发的,具有很好的跨平台特性,并且可以 ...
- vowels_单元音
vowels(美式): 单元音: [i]:需要用劲喊出类似于“yi”的四声,费力咧开嘴,单词eat.need.thief.meet. [?]:卷舌音,单词bird.her.worry.certain. ...
- rails应用无法读取kafka数据报错Kafka::Error: Failed to find group coordinator
如果确保kafka中有数据,rails应用中却无法读取到,或报如下错误: Kafka::Error: Failed to find group coordinator 一般有两种情况,解决: ...