题目:题目链接
题意:就是按照题目给出的优先级把信息在PUT命令下存入队列,在GET命令下输出优先级最高的信息;
分析:优先级队列,使用node型+运算符重载,这样就可以按照自己的意愿来排序队列中的信息了:
代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
#define INF 0x7fffffff
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
struct node
{
char str[100];
int s1;
int s2;
int cnt;
friend bool operator < (node a, node b)
{
if(a.s2 != b.s2)
return a.s2 > b.s2;
else
return a.cnt > b.cnt;
}
};
int main()
{
int k = 0;
char op[5];
node NODE;
priority_queue<node> Q;
while(scanf("%s", op) != EOF)
{
if(op[0] == 'G')
{
if(Q.empty())
{
printf("EMPTY QUEUE!\n");
continue;
}
else
{
NODE = Q.top();
Q.pop();
printf("%s %d\n", NODE.str, NODE.s1);
}
}
else if(op[0] == 'P')
{
scanf("%s%d%d", NODE.str, &NODE.s1, &NODE.s2);
NODE.cnt = k++;
Q.push(NODE);
}
}
return 0;
}