今天写了几个优先队列的题目
涉及到设置优先级 就去现学了一点
我还不是很熟悉 就先记录下基础模板吧
等学会了重载运算符再细细总结
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#define ll long long
using namespace std;
const int N=6e4+100;
struct node
{
int val;
int num;
friend bool operator <(const node &a,const node &b)
{///记得这个比较和逻辑是相反的
if(a.val==b.val)
return a.num>b.num;
else
return a.val<b.val;
}
};
int main()
{
int n;
while(~scanf("%d",&n))
{
priority_queue<node>pq[4];
char ope[10];
int id;
int cnt=0;
for(int i=1;i<=n;i++)
{
getchar();
scanf("%s",ope);
if(ope[0]=='I')
{
node tmp;
scanf("%d %d",&id,&tmp.val);
tmp.num=++cnt;
pq[id].push(tmp);
}
else
{
scanf("%d",&id);
if(pq[id].empty())
cout<<"EMPTY"<<endl;
else
{
cout<<pq[id].top().num<<endl;
pq[id].pop();
}
}
}
}
return 0;
}
注意这一点
下面定义时写priority_queuepq即可
先讲讲这个优先级的设置 首先这个设置是与sort以及逻辑相悖的
这个设置的是优先排列val大的在前面 在val相等的情况下 优先使num小的排在前面
记住就好
再挂一个别的题目的 加深影响 只挂片段吧
那就先到这里了 简单的优先级就先按照这个模板写 以后再优化吧
来源:CSDN
作者:小张今天AC了吗
链接:https://blog.csdn.net/leoxe/article/details/104401151