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~最小堆。应该是相同的优先级的时候,按照输入顺序输出

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值