模拟题,有入队出队操作,每次出队的时候根据不同的策略,选择优先级最小的出队或者优先级最大的出队。
两个优先级队列,一个模拟大顶堆,一个模拟小顶堆,出队的时候按照要求出队就可以了。
但是用两个队列模拟一个队列有个同步性问题,可以用一个数组vis表示该元素是否已在真实的队列中出列,这样就解决了两个队列模拟一个队列操作的同步问题。
详情见代码(个人觉得代码比较挫,要是再复杂一点的模拟题就直接跪了):
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 100030
using namespace std;
bool vis[maxn];
int n,ia,a[maxn];
priority_queue<int,vector<int>,greater<int> >miq;
priority_queue<int,vector<int>,less<int> > maq;
int cmd=1;
bool init()
{
if(scanf("%d",&n)!=EOF)
{
scanf("%d",&ia);
for(int i=0;i<ia;i++)
{
scanf("%d",&a[i]);
}
return true;
}
return false;
}
void solve()
{
int counta=0,now=0;
memset(vis,0,sizeof(vis));
while(!miq.empty())miq.pop();
while(!maq.empty())maq.pop();
cmd=1;
char s[10];
int num;
bool endp=false;
while(!endp)
{
scanf("%s",s);
switch (s[0])
{
case 'a':scanf("%d",&num);vis[num]=1;miq.push(num);maq.push(num);break;
case 'r':
num=-1;now++;
if(cmd==1)
{
while(!miq.empty())
{
int temp=miq.top();miq.pop();
if(vis[temp])
{
num=temp;
vis[num]=0;
break;
}
}
}
if(cmd==2)
{
while(!maq.empty())
{
int temp=maq.top();maq.pop();
if(vis[temp])
{
vis[temp]=0;
num=temp;
break;
}
}
}
if(now==a[counta])
{
counta++;
printf("%d\n",num);
}
break;
case 'p':scanf("%d",&num);cmd=num;break;
case 'e':endp=true;break;
default:while(1);
}
}
}
int main()
{
bool f=false;
while(init())
{
if(f)
{
printf("\n");
}
else
{
f=true;
}
solve();
}
return 0;
}