标签:exactly seq 行数据 har contains always node -o enter
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 (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.
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*
就是说有一辆公交车,有n排;
接下来n个数据代表n排的权值(每排只能坐2个人);
然后接下来又2*n的数据
代表要坐入这辆公交车的人的类型。
1代表外向型,0代表内向型。
外向型:优先选择旁边有人的且权值最大的位置,如果没有的话,选择权值最大的位置。
内向型:优先选择没人的位置且权值最小的位置。
我使用了四个优先队列进行数据的维护。
第一个队列,按权值从小到大维护;
第二个队列,按权值从大到小维护;(ps:代码实现的时候,我使用第一个维护大的,第二个维护小的)
第三个队列,代表被选择的位置,按从小到大维护;(ps:其实第三个队列意义不大,因为后面没有使用的)
第四个队列,代表被选择的位置,按从大到小维护;
使用了一个位置来标记是否使用过。
输入0,判断第一个队列是否还有值,有的话,压入第三个队列,和第四个队列
代表被使用了。顺带把答案放到输出数组里。
输入1,判断第四个队列中是否有值,有的话,就选择维护的第一个。因为肯定是
有人的最大权值。把第一个放入输出数组。
如果第四个队列中没有值,那就只能从第二队列中取出第一个。把(第一个)答案放到输出数组中。
(其实这个题目给的数据都是合法的。因为他保证0都坐得进去。)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 2*1e5+5;
typedef struct NODE
{
int math;
int flag;
friend bool operator < (NODE a, NODE b)
{
return a.math > b.math; //重载小于号使得小的先出队列
}
} node;
node team[MAXN];
node team1[MAXN];
int bj[MAXN];
int people[MAXN*2];
priority_queue<node>que1;///保存大的值
priority_queue<node>que2;///保存小的值
priority_queue<node>que3;///保存结果
priority_queue<node>que4;///保存另一个结果
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(team,0,sizeof(team));
memset(bj,0,sizeof(bj));
memset(people,0,sizeof(people));
while(!que3.empty())
{
que3.pop();
}
while(!que1.empty())
{
que1.pop();
}
while(!que2.empty())
{
que2.pop();
}
while(!que4.empty())
{
que4.pop();
}
for(int i=0; i<n; i++)
{
scanf("%d",&team[i].math);
team1[i].math=-team[i].math;
team1[i].flag=i;
team[i].flag=i;
que1.push(team1[i]);
que2.push(team[i]);
}
char str[MAXN*2];
scanf("%s",str);
for(int i=0; i<n*2; i++)
{
int ans;
ans=str[i]-‘0‘;
if(ans==0)
{//printf("!!!!\n");
if(!que1.empty())
{
node ans1=que2.top();
//printf("%d\n",ans1.math);
que2.pop();
if(bj[ans1.flag]==0)
{
que3.push(ans1);
bj[ans1.flag]=1;
people[i]=ans1.flag+1;
ans1.math=-ans1.math;
que4.push(ans1);
}
}
}
if(ans==1)
{//printf("..?.\n");
if(!que4.empty())
{
node ans2;
ans2=que4.top();
que4.pop();
people[i]=ans2.flag+1;
}
else
if(que4.empty())
{
node ans3;
ans3=que1.top();
que1.pop();
bj[ans3.flag]=1;
people[i]=ans3.flag+1;
}
}
}
for(int i=0;i<2*n;i++)
printf("%d ",people[i]);
puts("");
}
return 0;
}
Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)
标签:exactly seq 行数据 har contains always node -o enter
原文地址:https://www.cnblogs.com/qq136155330/p/9069895.html