#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int maxn=60000+10; int heap_size=0; //堆的元素个数 struct Node{ char name[100]; int para,pri; int t; //用于存储信息加入的顺序,当优先级相同时,t小的先出队列 }node[maxn]; //交换node[a]和node[b]的值 void exchange(int a,int b){ Node tmp; tmp=node[a]; node[a]=node[b]; node[b]=tmp; } //比较node[a]和node[b],若a小于,则返回-1。 int compare(int a,int b){ if(node[a].pri<node[b].pri) return -1; if(node[a].pri>node[b].pri) return 1; if(node[a].t<node[b].t) return -1; if(node[a].t>node[b].t) return 1; return 0; } //维护堆,使堆保持最小堆的性质 void heap_update(int i){ int small; int l=2*i; int r=2*i+1; if(l<=heap_size && compare(l,i)<0) small=l; else small=i; if(r<=heap_size && compare(r,small)<0) small=r; if(small!=i){ exchange(i,small); heap_update(small); } } //加入一个新元素 void heap_insert(char str[],int para,int pri){ heap_size++; strcpy(node[heap_size].name,str); node[heap_size].para=para; node[heap_size].pri=pri; node[heap_size].t=heap_size; int t=heap_size; while(t>1 && node[t/2].pri>node[t].pri){ exchange(t/2,t); t=t>>1; } } //将堆顶元素出队列 Node heap_pop(){ Node tmp=node[1]; node[1]=node[heap_size]; heap_size--; heap_update(1); return tmp; } int main() { char str[5],s[110]; Node tmp; int para,pri; while(scanf("%s",str)!=EOF){ if(str[0]=='G'){ if(heap_size==0){ printf("EMPTY QUEUE! "); } else{ tmp=heap_pop(); printf("%s %d ",tmp.name,tmp.para); } } else{ scanf("%s%d%d",s,¶,&pri); heap_insert(s,para,pri); } } return 0; }