题目链接~~>
这题是接触优先队列的第二题代码有点。。。
开始做时一直wa,最后才明白应该把 priority_queue<zha>q1;
等放到循环里面去,。。。
代码:
#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct zha { int a1; int h1; friend bool operator<(const zha &a,const zha &b) { if(a.a1!=b.a1) return a.a1 < b.a1 ; else return a.h1 > b.h1; } }; struct zhan { int a2; int h2; friend bool operator<(const zhan &a,const zhan &b) { if(a.a2!=b.a2) return a.a2 < b.a2 ; else return a.h2 > b.h2; } }; struct zhang { int a3; int h3; friend bool operator<(const zhang &a,const zhang &b) { if(a.a3!=b.a3) return a.a3 < b.a3 ; else return a.h3 > b.h3; } }; int main() { char s[10]; int T,n1,n2;int k; while(scanf("%d",&T)!=EOF) { priority_queue<zha>q1; priority_queue<zhan>q2; priority_queue<zhang>q3; zha current1; zhan current2; zhang current3; k=1; while(T--) { scanf("%s",s); if(s[0]=='I') { scanf("%d%d",&n1,&n2); if(n1==1) { current1.a1=n2; current1.h1=k; q1.push(current1); } else if(n1==2) { current2.a2=n2; current2.h2=k; q2.push(current2); } else if(n1==3) { current3.a3=n2; current3.h3=k; q3.push(current3); } k++; } else { scanf("%d",&n1); if(n1==1) { if(q1.empty()) { printf("EMPTY\n"); } else { current1=q1.top(); printf("%d\n",current1.h1); q1.pop(); } } else if(n1==2) { if(q2.empty()) { printf("EMPTY\n"); } else { current2=q2.top(); printf("%d\n",current2.h2); q2.pop(); } } else if(n1==3) { if(q3.empty()) { printf("EMPTY\n"); } else { current3=q3.top(); printf("%d\n",current3.h3); q3.pop(); } } } } } return 0; }
别人代码:
/********************************* * 日期:2013-3-16 * 作者:SJF0115 * 题号: HDU 题目1873: 看病要排队 * 来源:http://acm.hdu.edu.cn/showproblem.php?pid=1873 * 结果:AC * 来源:2008浙大研究生复试热身赛(2)——全真模拟 * 总结: **********************************/ #include<iostream> #include<stdio.h> #include<queue> using namespace std; struct Patient { //值 int priority; //编号 int key; //重载操作符 friend bool operator < (Patient p1,Patient p2) { if(p1.priority != p2.priority){ return p1.priority < p2.priority; } else{ return p1.key > p2.key; } } }; int main(){ int i,N,k; char Type[4]; int DoctorID,PatientID; Patient patient[2001]; while(scanf("%d",&N) != EOF){ //定义三个医生 priority_queue<Patient> Doctor1; priority_queue<Patient> Doctor2; priority_queue<Patient> Doctor3; k = 1; while(N--){ scanf("%s",Type); //诊治 if(strcmp(Type,"IN") == 0){ //输入病人和医生 patient[k].key = k; scanf("%d %d",&DoctorID,&patient[k].priority); //排队 if(DoctorID == 1){ Doctor1.push(patient[k]); } else if(DoctorID == 2){ Doctor2.push(patient[k]); } else{ Doctor3.push(patient[k]); } k++; } //出院 else if(strcmp(Type,"OUT") == 0){ //医生DoctorID进行了一次诊治,诊治完毕后,病人出院 scanf("%d",&DoctorID); //医生1 if(DoctorID == 1){ if(Doctor1.empty()){ printf("EMPTY\n"); } else{ printf("%d\n",Doctor1.top().key); //出院 Doctor1.pop(); } } //医生2 else if(DoctorID == 2){ if(Doctor2.empty()){ printf("EMPTY\n"); } else{ printf("%d\n",Doctor2.top().key); //出院 Doctor2.pop(); } } //医生3 else{ if(Doctor3.empty()){ printf("EMPTY\n"); } else{ printf("%d\n",Doctor3.top().key); //出院 Doctor3.pop(); } } } } } return 0; }
别人代码:
#include <cstdio> #include <algorithm> #include <queue> using namespace std; struct Node { int num,id; friend bool operator < (Node a,Node b) { if(a.num!=b.num) return a.num<b.num; else return a.id>b.id; } }; int main() { int n,A,B,cnt; char str[5]; Node t; while(~scanf("%d",&n)) { cnt=0; priority_queue <Node>q[4]; while(n--) { scanf("%s",str); if(str[0]=='I'){ scanf("%d%d",&A,&B); t.id=++cnt; t.num=B; q[A].push(t); } else{ scanf("%d",&A); if(!q[A].empty()){ t=q[A].top(); q[A].pop(); printf("%d\n",t.id); } else printf("EMPTY\n"); } } } return 0; }