Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B
2 seconds
256 megabytes
standard input
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.
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.
一、原题地址
http://codeforces.com/contest/982/problem/B
二、大致题意
一辆公交有n排座位,每排座位有两个椅子。
接着给出每排座位的宽度。
这时有2*n个人要上车,这群人有两种类型。
1、内向的人:会优先选择座位宽度最小的那排就坐,最好旁边是没人的。
2、外向的人:会优先选择座位旁边有人的那排就坐,在此基础上座位选择越大越好。
三、思路
我们可以先把座位的宽度进行排序。
1、当内向的人上车时,我们记录一个pos表示当前内向的人最优的位置,同时将座位的编号存入栈。
2、当外向的人上车时,我们提取栈内最顶端的元素,因为内向的人是按照座位宽度从小到大入座的,所以此时的栈顶元素,必定为有内向的人就坐的宽度最大的位置。同时删除栈顶元素,表示这个位置被使用了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return a == ? b : gcd(b % a, a); } int n;
struct SEAT
{
int w, id;
}seat[];
bool cmp(SEAT xx, SEAT yy)
{
return xx.w < yy.w;
}
int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &seat[i].w);
seat[i].id = i;
}
sort(seat + , seat + + n, cmp);
char s[ * ];
scanf("%s", s);
int pos0 = ;
stack<int>sta;
for (int i = ; i < * n; i++)
{
if (s[i] == '')
{
printf("%d ", seat[pos0].id);
sta.push(seat[pos0].id);
pos0++;
}
else if (s[i] == '')
{
printf("%d ", sta.top());
sta.pop();
}
}
getchar();
getchar();
}
Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B的更多相关文章
- Codeforces Codeforces Round #484 (Div. 2) E. Billiard
Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...
- Codeforces Codeforces Round #484 (Div. 2) D. Shark
Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...
- Codeforces Round #484 (Div. 2)
题目链接:http://codeforces.com/contest/982 A. Row time limit per test:1 second memory limit per test:256 ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #436 (Div. 2)C. Bus 模拟
C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input out ...
- Codeforces Round #369 (Div. 2) A. Bus to Udayland 水题
A. Bus to Udayland 题目连接: http://www.codeforces.com/contest/711/problem/A Description ZS the Coder an ...
- Codeforces Round #369 (Div. 2) A. Bus to Udayland (水题)
Bus to Udayland 题目链接: http://codeforces.com/contest/711/problem/A Description ZS the Coder and Chris ...
- Codeforces Round #436 (Div. 2) C. Bus
http://codeforces.com/contest/864/problem/C 题意: 坐标轴上有x = 0和 x = a两点,汽车从0到a之后掉头返回,从a到0之后又掉头驶向a...从0到a ...
- Codeforces Round #491 (Div. 2) E - Bus Number + 反思
E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是 ...
随机推荐
- CSDN Markdown 超链接
CSDN Markdown 的超链接总是在当前页面打开新的链接,后来发现了一种可以在新窗口打开超链接的语法,如下: <a href="https://zh.wikipedia.org/ ...
- Spark Streaming消费Kafka Direct保存offset到Redis,实现数据零丢失和exactly once
一.概述 上次写这篇文章文章的时候,Spark还是1.x,kafka还是0.8x版本,转眼间spark到了2.x,kafka也到了2.x,存储offset的方式也发生了改变,笔者根据上篇文章和网上文章 ...
- 归并排序(从上到下、从下到上)——C语言
归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归 ...
- MySQL5.7运行CPU达百分之400处理方案
用户在使用 MySQL 实例时,会遇到 CPU 使用率过高甚至达到 100% 的情况.本文将介绍造成该状况的常见原因以及解决方法,并通过 CPU 使用率为 100% 的典型场景,来分析引起该状况的原因 ...
- Windows 下安装 Python + Django
Django是Python的一个Web开发框架,以下是介绍的是windows下的安装步骤, 作者的环境是Win10 ,Windows Server 也是一样的 以下是作者整理的步骤,也可以参考官方教程 ...
- 数据结构之二叉树的构建C++版
二叉树的构建要注意与链式表的区别,二叉树这里的构建十分低级,每个树只是构建了一个单一的二叉树节点,总体来看是有下向上构建的.用户需要手动去构建自己需要的树,而不是直接去插入数据就到二叉树中了,因为不是 ...
- C# 发送邮件 并自定义邮件格式
话不多说,直接上代码 //邮件提醒 string Body = @"Dear " + list.Rows[i]["people"] + ":<b ...
- 100天搞定机器学习|Day35 深度学习之神经网络的结构
100天搞定机器学习|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机器学习 ...
- java读取本机磁盘及遍历磁盘文件
1. 获取本机所有盘符信息 //1. 获取本机盘符 File[] roots = File.listRoots(); for (int i = 0; i < roots.length; i++) ...
- EVE-NG入门篇
目录 一.EVE-NG配置要求 二.EVE-NG 安装 三.基于OVA的安装步骤 四.导入设备介绍 五.启动设备 六.与secure CRT关联 七.常见问题 一.EVE-NG配置要求 1.最低配置 ...