- 一个内向者总是选择两个座位都没人的一排。 在这些排中,他选择座位宽度最小的,并占据了其中的一个座位;
- 一个外向型的人总是选择有人的一排。 在这些排中,他选择了座位宽度最大的那个,并占据了空位。
Input
Output
打印 2n 个整数 - 乘客将坐的排。 乘客的顺序应与输入的顺序相同。
Sample Input
Input
2 3 1 0011
Output
2 1 1 2
Input
6 10 8 9 11 13 5 010010011101
Output
6 6 2 3 3 1 4 4 1 2 5 5
Hint
在第一个例子中,第一个乘客(内向)选择第二排,因为它具有最小宽度的座位。 第二位乘客(内向)选择第1行,因为它现在是唯一的空行。 第三位乘客(外向型)选择第一排,因为它只有一个占用的座位,座位宽度是这些排中最大的。 第四位乘客(外向性)选择第二排,因为它是唯一一个有空位的排。
双端队列: 头进 1 2 3 4 尾
单项队列 : 进 4 3 2 1
双端队列:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int n;
pair<int, int> a[MAXN];
char str[MAXN*2];
deque<int> S;
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i].first);
a[i].second = i+1;
}
sort(a, a+n);
scanf("%s", str);
int cnt = 0;
for (int i = 0; i < 2*n; i++)
{
if (str[i] == '0')
{
printf("%d ", a[cnt].second);
S.push_front(a[cnt].second);
cnt++;
}
if (str[i] == '1')
{
printf("%d ", S.front());
S.pop_front();
}
}
return 0;
}
个人做法时间超限:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct p
{
int a;
int ge;
int i;
} x[200010];
int cmp(struct p a,struct p b)
{
if(a.a!=b.a)
return a.a<b.a;
}
int main()
{
int n,t,i;
char a[400010];
scanf("%d",&n);
for(i=0; i<=n-1; i++)
{
scanf("%d",&x[i].a);
x[i].ge=0;
x[i].i=i+1;
}
getchar();
sort(x,x+n,cmp);
gets(a);
int b=strlen(a);
for(i=0; i<=b-1; i++)
{
if(a[i]=='0')
{
t=0;
while(1)
{
if(x[t].ge==0)
{
x[t].ge++;
if(i!=b-1)
printf("%d ",x[t].i);
else
printf("%d\n",x[t].i);
break;
}
else
t++;
}
}
if(a[i]=='1')
{
t=n-1;
while(1)
{
if(x[t].ge==1)
{
x[t].ge++;
if(i!=b-1)
printf("%d ",x[t].i);
else
printf("%d\n",x[t].i);
break;
}
else
t--;
}
}
}
return 0;
}