/*优先队列的小根堆的写法。水题。
还有一个方法是将所有的值都变为负数,然后直接用优先队列。
输出输入和输出还有字符串用c,不然tle。*/
#include <iostream>
#include <queue>
#include <cstring>
#include <stdio.h>
using namespace std;
int main()
{
priority_queue<int,vector<int>,greater<int> > v;
char s[20];
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
if(strcmp(s,"add")==0)
{
scanf("%d",&n);
v.push(n);
}
else if(s[0]=='d')
{
if(v.empty()) continue;
else v.pop();
}
else
{
if(!v.empty())
printf("%d\n",v.top());
else continue;
}
}
return 0;
}
本文介绍了一种使用小根堆实现优先队列的方法,并通过C++代码示例展示了如何进行元素的添加、删除及获取最小元素的操作。此外,还提供了一种将数值转为负数以简化实现的技巧。
1143

被折叠的 条评论
为什么被折叠?



