Codeforces 982B (優先佇列)
題面:
B. Bus of Characterstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn 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 2
- 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.
InputThe 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≤w
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.
OutputPrint 2n2n integers — the rows the passengers will take. The order of passengers should be the same as in input.
ExamplesinputCopy2 3 1 0011outputCopy
2 1 1 2inputCopy
6 10 8 9 11 13 5 010010011101outputCopy
6 6 2 3 3 1 4 4 1 2 5 5Note
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行的椅子,每行有兩個椅子,第i行的椅子的寬度為wi。
第二行輸入一個長度為2*n字串,代表兩種人,0表示第一種人,他們只會選擇坐在一行沒人的位子上,如果有多個地方滿足條件,他會選擇寬度最小的凳子;1表示第一種人,他們只會選擇在坐在一行有一個人坐的位子上,如果有多個地方滿足條件,則他們會優先選擇寬度最大的凳子。詢問這2*n個人分別坐在在哪一行。
題目分析:
因為我們知道,1這類人一定是要坐在有一個人的地方的,因此,我們可以分析到,第一個坐下的必定是0的人。因為他們每次要坐在寬度最小的凳子上,因此我們在這裡可以建立一個小根堆,每次遇到0的時候就將堆頂元素pop出;同時,因為這個人坐下後,導致了這個凳子可以被1這類人所坐,(而這類人要坐在寬度最大的凳子上)因此我們可以再建立一個大根堆,每次pop掉小根堆的元素後,將該元素再push進大根堆,當遇到1的時候再pop出大根堆堆頂元素即可。
程式碼:
#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef pair<int,int>PLL;
int main()
{
priority_queue<PLL,vector<PLL>,greater<PLL> >que0;
priority_queue<PLL>que1;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int tmp=0;
cin>>tmp;
que0.push(PLL(tmp,i));
}
string str;
cin>>str;
int len=str.length();
for(int i=0;i<len;i++){
if(str[i]=='0'){
if(i==0){
cout<<que0.top().second;
que1.push(que0.top());
que0.pop();
}
else{
cout<<" "<<que0.top().second;
que1.push(que0.top());
que0.pop();
}
}
else{
cout<<" "<<que1.top().second;
que1.pop();
}
}
return 0;
}