明显的优先队列

用了STL,但是不全懂,自己写个堆估计悬... 有工具咱就用!

#include <iostream>
#include
<queue>
#include
<vector>
#include
<stdio.h>

using namespace std;
const int MAXN = 60001;

struct message
{
char name[20];
int para,pri;
int order;
bool operator < (const message& m1)const
{
if(pri == m1.pri)
return order > m1.order;
return pri < m1.pri;
}
};

struct cmp
{
bool operator()(const message &m1, const message &m2){
if(m1.pri == m2.pri)
return m1.order > m2.order;
return m1.pri > m2.pri;
}
};
//以上全不懂,全是试出来的,C++ stl 没时间看啊!
priority_queue<message,vector<message>,cmp> q;

int main(){
char str[5],name[20];
int para,pri,cnt = 1;
while(scanf("%s",str) == 1){
switch (str[0])
{
case 'G':
if(q.empty())
printf(
"EMPTY QUEUE!\n");
else {
message mout
= q.top();
q.pop();
printf(
"%s %d\n",mout.name,mout.para);
}
break;
case 'P':
scanf(
"%s%d%d",name,&para,&pri);
message m ;
strcpy(m.name,name);
m.para
= para;
m.pri
= pri;
m.order
= cnt++;
q.push(m);
break;
}

}
return 0;
}

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-11-02
  • 2021-09-29
  • 2021-10-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-10
  • 2021-07-27
  • 2022-12-23
  • 2021-12-09
  • 2022-01-21
相关资源
相似解决方案