我们可以先把座位的宽度进行排序。
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 == 0 ? b : gcd(b % a, a); }
int n;
struct SEAT
{
int w, id;
}seat[300005];
bool cmp(SEAT xx, SEAT yy)
{
return xx.w < yy.w;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &seat[i].w);
seat[i].id = i;
}
sort(seat + 1, seat + 1 + n, cmp);
char s[300005 * 2];
scanf("%s", s);
int pos0 = 1;
stack<int>sta;
for (int i = 0; i < 2 * n; i++)
{
if (s[i] == '0')
{
printf("%d ", seat[pos0].id);
sta.push(seat[pos0].id);
pos0++;
}
else if (s[i] == '1')
{
printf("%d ", sta.top());
sta.pop();
}
}
getchar();
getchar();
}