1. 程式人生 > >Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

flag ron 數據 names rac AR string 輸入 實現

Codeforces Round #484 (Div. 2) B. Bus of Characters

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版)