Bus of Characters(cf 982B)

本文介绍了一种基于贪心策略的公交座位分配算法,该算法针对不同性格乘客(内向与外向)选择座位的行为特点进行优化。通过预先排序座位宽度并使用栈和队列结构,实现了高效座位分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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.

Sample Input

2
3 1
0011
Sample Output

2 1 1 2 
Sample Input

6
10 8 9 11 13 5
010010011101

Sample Output

6 6 2 3 3 1 4 4 1 2 5 5 

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

所以先把座位从小到大排列一下,遇到0就顺序压入队列,并且把这个值再压入栈,遇到1就读取栈的top压入队列。(用数组存出现了一个bug,还在修改ing)

代码如下:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define maxn 10007
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.00000001
using namespace std;
typedef long long ll;
struct st
{
    long long int num;
    long long int big;
}a[200001],v;
bool cmp(st a,st b)
{
    return a.big<b.big;
}
int main()
{
	stack<st> sk;
	queue<int> q;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
	cin>>a[i].big;
    	a[i].num=i+1;
    }
    sort(a,a+n,cmp);
    int t=0,l=0;
    for(int i=0;i<2*n;i++)
	{
		char k;
		cin>>k;
		if(k=='0')
		{
			sk.push(a[t]);
			q.push(a[t].num);
			t++;
		}
		if(k=='1')
		{
			v=sk.top();
			sk.pop();
			q.push(v.num);
		}
	}
	while(!q.empty())
	{
		cout<<q.front()<<" ";
		q.pop();
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值