传送门
优先队列在语法(推入,删除)上与普通队列一样,不同在于声明时要这样:priority_queue
优先队列中默认出来的top值为该队列中的最小的数.这道题就是一道模拟优先队列的题.(被人看来是水题,而我却。)
代码如下:
#include
#include
#include
using namespace std;
const int maxn=1000005;
struct node
{
char ch;
int x;
}op[maxn];
int main()
{
priority_queue,greater >q;//优先队列的严格声明方式.
int n,i,k=0,u;//默认该队列中最小的数为优先级最高的数,即q.top=q.min.
char s[10];
scanf("%d",&n);
for(int i=0;iu)
{
op[k].x=u;
op[k++].ch='i';
q.push(u);
op[k].x=u;
op[k++].ch='g';
}
else{
while(!q.empty()&&q.top()u || q.empty())
{
op[k].x=u;
op[k++].ch='i';
q.push(u);
}
op[k].x=u;
op[k++].ch='g';
}
}
}
printf("%d\n",k);//总共最少步骤
for(int i=0;i