//2365768 2010-12-06 16:41:01 Accepted 2724 C 280 1332 VRS
//2724 模拟消息队列 用堆实现的优先队列
//练堆的基础题
#include<stdio.h>
#include<string.h>
#define MAXNUM 60005
#define bool int
typedef struct _MESSGAE
{
char name[10];
int para;
int key;
}MESSAGE;
MESSAGE process[MAXNUM];
void swap(int x,int y)
{
strcpy(process[0].name,process[x].name);
process[0].para=process[x].para;
process[0].key=process[x].key;
strcpy(process[x].name,process[y].name);
process[x].para=process[y].para;
process[x].key=process[y].key;
strcpy(process[y].name,process[0].name);
process[y].para=process[0].para;
process[y].key=process[0].key;
}
int n;
void HeapUp(int k)
{
int father,son;
son=k;
do{
father=son/2;
if(process[father].key>process[son].key)
swap(father,son);
else
break;
son=father;
}while(son>1);
}
void HeapDown(int k)
{
int father,son;
father=k;
while(father<=n/2)
{
son=2*father;
if(son+1<=n && process[son].key>process[son+1].key)
son++;
if(process[father].key>process[son].key)
swap(father,son);
else
break;
father=son;
}
}
void Insert(char* name,int para,int key)
{
n++;
strcpy(process[n].name,name);
process[n].para=para;
process[n].key=key;
HeapUp(n);
}
int Top()
{
return 1;
}
void Pop()
{
swap(1,n);
n--;
HeapDown(1);
}
bool IsEmpty()
{
if(n==0)
return 1;
else
return 0;
}
int main()
{
int temp;
char option[10];
n=0;
while(scanf("%s",&option)!=EOF)
{
if(strcmp(option,"GET\0")==0)
{
if(IsEmpty()==1)
printf("EMPTY QUEUE!\n");
else
{
temp=Top();
printf("%s %d\n",process[temp].name,process[temp].para);
Pop();
}
}
else
{
scanf("%s%d%d",&process[0].name,&process[0].para,&process[0].key);
Insert(process[0].name,process[0].para,process[0].key);
}
}
return 0;
}