3.给出优先队列的实现,实现4个操作
• ADD N P:往队列里加入id为N的优先级为P的任务
• NEXT:输出下一个最高优先级的任务的id,如果优先级相同输出id小的任务,若队列中没有任务输出-1
• REMOVE N:移除id为N的任务
• COUNT:输出队列中的任务数量
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#pragma warning(disable:4996);
using namespace std;
struct task {
int id;
int prior;
friend bool operator<(task& t1,task& t2)
{
if (t1.prior != t2.prior)
{
return t1.prior > t2.prior;
}
else {
return t1.id < t2.id;
}
}
}t;
vector<task> T;
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
string str;
cin >> str;
if (str == "ADD") {
scanf("%d %d", &t.id, &t.prior);
T.push_back(t);
sort(T.begin(), T.end());
}
else if (str == "NEXT") {
if (T.size() == 0)
{
printf("-1\n");
}
else {
task temp = T[0];
printf("%d\n", temp.id);
T.erase(T.begin());
}
}
else if (str == "REMOVE") {
int tempID;
scanf("%d", &tempID);
vector<task>::iterator it = T.begin();
while (it != T.end())
{
if (it->id == tempID)
{
it = T.erase(it);
}
else {
it++;
}
}
}
else if (str == "COUNT") {
printf("%d\n", T.size());
}
}
}