#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
const int M = 60005;
struct node{
char name[50];
int para;
int pri,t;
}p[M];
int heap[M];
int used, top;
int check(int p1, int p2) {
if(p[p1].pri < p[p2].pri)
return -1;
if(p[p1].pri > p[p2].pri)
return 1;
if(p[p1].t < p[p2].t)
return -1;
if(p[p1].t > p[p2].t)
return 1;
return 0;
}
int main()
{
char str[100];
used = 0;
top = 0;
int cnt = 0;
while(scanf("%s", str) != EOF) {
if(strcmp(str,"GET") == 0){
if(top){
printf("%s %d\n", p[heap[1]].name, p[heap[1]].para);
int k = 1;
heap[k] = heap[top--];
while(k * 2 <= top) {
int t = k*2;
if(t < top && check(heap[t+1], heap[t]) < 0)
t++;
if(check(heap[t], heap[k]) < 0){
swap(heap[t], heap[k]);
k = t;
}else{
break;
}
}
}else {
printf("EMPTY QUEUE!\n");
}
}else {
scanf("%s%d%d", p[used].name, &p[used].para, &p[used].pri);
p[used].t = cnt++;
int k = ++top;
heap[k] = used++;
while(k > 1) {
int t = k/2;
if(check(heap[t], heap[k]) > 0){
swap(heap[t], heap[k]);
k = t;
}else {
break;
}
}
}
}
return 0;
}
zoj2724 堆的插入调整
最新推荐文章于 2020-06-17 11:17:39 发布