#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std; typedef struct minheap *Heap;
struct minheap
{
int last,max;
int data[];
char str[][];
int para[];
}Minheap; void HeapInset(int x,Heap H,char s[],int pa)
{
int i; i=++H->last;
while(i!=&&x<H->data[i/])
{
H->data[i]=H->data[i/];
strcpy(H->str[i],H->str[i/]);
H->para[i]=H->para[i/];
i/=;
}
H->data[i]=x;
strcpy(H->str[i],s);
H->para[i]=pa;
} int deleMin(Heap H)
{
int i,ci;
int x,y;
char str[];
int pa;
x=H->data[];//堆中最小元 strcpy(str,H->str[H->last]);
pa=H->para[H->last];
y=H->data[H->last]; H->last--; i=;
ci=;
//cout<<"last="<<H->last<<endl;
while(ci<=H->last)
{
//cout<<"ci="<<ci<<"y="<<y<<endl<<" "<<H->data[ci]<<endl;
if(ci<H->last&&H->data[ci+]<H->data[ci])ci++;
if(H->data[ci]>y)break; H->data[i]=H->data[ci];
strcpy(H->str[i],H->str[ci]);
H->para[i]=H->para[ci]; i=ci;
ci*=;
} H->data[i]=y;
strcpy(H->str[i],str);
H->para[i]=pa;
int tmp;
if(H->data[i]==H->data[i+])
{
tmp=H->data[i];H->data[i]=H->data[i+];H->data[i+]=tmp;
tmp=H->para[i];H->para[i]=H->para[i+];H->para[i+]=tmp;
strcpy(str,H->str[i]);strcpy(H->str[i],H->str[i+]);
strcpy(H->str[i+],str);
} return x;
}
int main()
{
char str[],s1[];
int para,data;
Heap H;
H=(Heap)malloc(sizeof(Minheap));
H->last=;
H->max=;
while(cin>>s1)
{ if(s1[]=='P')
{
cin>>str>>para>>data;
HeapInset(data,H,str,para); //cout<<"insert: "<<str<<" "<<para<<" "<<data<<endl; //cout<<"text:"<<H->str[1]<<" "<<H->data[1]<<endl;
}
else if(s1[]=='G')
{
if(H->last==)
printf("EMPTY QUEUE!\n");
else
{
cout<<H->str[]<<" "<<H->para[]<<endl; deleMin(H);
//cout<<"last:"<<H->str[H->last]<<" "<<H->data[H->last]<<endl;
//cout<<"last-1:"<<H->str[H->last-1]<<" "<<H->data[H->last-1]<<endl;
} }
//cout<<H->last<<endl;
}
return ;
}
一直WA~最小堆。应该是相同的优先级的时候,按照输入顺序输出