HDOJ 1590

  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<stdlib.h>
  4 #include<string.h>
  5 using namespace std;
  6 
  7 typedef struct minheap *Heap;
  8 struct minheap
  9 {
 10     int last,max;
 11     int data[60001];
 12     char str[60001][100];
 13     int para[6001];
 14 }Minheap;
 15 
 16 void HeapInset(int x,Heap H,char s[],int pa)
 17 {
 18     int i;
 19     
 20     i=++H->last;
 21     while(i!=1&&x<H->data[i/2])
 22     {
 23         H->data[i]=H->data[i/2];
 24         strcpy(H->str[i],H->str[i/2]);
 25         H->para[i]=H->para[i/2];
 26         i/=2;
 27     }
 28     H->data[i]=x;
 29     strcpy(H->str[i],s);
 30     H->para[i]=pa;    
 31 }
 32 
 33 
 34 int deleMin(Heap H)
 35 {
 36     int i,ci;
 37     int x,y;
 38     char str[110];
 39     int pa;
 40     x=H->data[1];//堆中最小元
 41     
 42     
 43     strcpy(str,H->str[H->last]);
 44     pa=H->para[H->last];
 45     y=H->data[H->last];
 46     
 47     H->last--;
 48     
 49     i=1;
 50     ci=2;
 51     //cout<<"last="<<H->last<<endl;
 52     while(ci<=H->last)
 53     {
 54         //cout<<"ci="<<ci<<"y="<<y<<endl<<" "<<H->data[ci]<<endl;
 55         if(ci<H->last&&H->data[ci+1]<H->data[ci])ci++;
 56         if(H->data[ci]>y)break;
 57 
 58         
 59         H->data[i]=H->data[ci];
 60         strcpy(H->str[i],H->str[ci]);
 61         H->para[i]=H->para[ci];
 62         
 63         i=ci;
 64         ci*=2;
 65     }
 66     
 67     H->data[i]=y;
 68     strcpy(H->str[i],str);
 69     H->para[i]=pa;
 70     int tmp;
 71     if(H->data[i]==H->data[i+1])
 72     {
 73         tmp=H->data[i];H->data[i]=H->data[i+1];H->data[i+1]=tmp;
 74         tmp=H->para[i];H->para[i]=H->para[i+1];H->para[i+1]=tmp;
 75         strcpy(str,H->str[i]);strcpy(H->str[i],H->str[i+1]);
 76         strcpy(H->str[i+1],str);
 77     }
 78     
 79     return x;
 80 }
 81 int main()
 82 {
 83     char str[100],s1[100];
 84     int para,data;
 85     Heap H;
 86     H=(Heap)malloc(sizeof(Minheap));
 87     H->last=0;
 88     H->max=61234;
 89     while(cin>>s1)
 90     {
 91         
 92         if(s1[0]=='P')
 93         {
 94             cin>>str>>para>>data;
 95             HeapInset(data,H,str,para);
 96             
 97             //cout<<"insert: "<<str<<" "<<para<<" "<<data<<endl;
 98             
 99             //cout<<"text:"<<H->str[1]<<" "<<H->data[1]<<endl;
100         }
101         else if(s1[0]=='G')
102         {
103             if(H->last==0)
104                 printf("EMPTY QUEUE!\n");
105             else 
106             {
107                 cout<<H->str[1]<<" "<<H->para[1]<<endl;
108                 
109                 deleMin(H);
110                 //cout<<"last:"<<H->str[H->last]<<" "<<H->data[H->last]<<endl;
111                 //cout<<"last-1:"<<H->str[H->last-1]<<" "<<H->data[H->last-1]<<endl;
112             }
113             
114         }
115         //cout<<H->last<<endl;    
116     }
117     return 0;    
118 }
View Code

一直WA~最小堆。应该是相同的优先级的时候,按照输入顺序输出

posted @ 2013-12-23 21:30  陈泽泽  阅读(202)  评论(0编辑  收藏  举报