zoj 2724Windows Message Queue

    技术2025-04-14  22

     

    杂题。。。用优先队列即可。

     

    需要自己写下比较函数,把入队顺序存到结构体里,如果优先级一样的话,按入队顺序排序。

     

    浙江省赛的题耶。。河南省赛还有3个月。。。加油啊~!

     

    #include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> #include <queue> #include <limits.h> using namespace std; typedef struct MES{ char str[25]; int mes; int p; int ind; }MES; bool operator<(MES a, MES b) { if( a.p == b.p ) return a.ind > b.ind; return a.p > b.p; } priority_queue<MES> q; int main() { char str[25]; int ind = 0; MES tmp; while( ~scanf("%s",str) ) { if( strcmp(str,"PUT") == 0 ) { scanf("%s%d%d",tmp.str,&tmp.mes,&tmp.p); tmp.ind = ind++; q.push(tmp); } if( strcmp(str,"GET") == 0 ) { if( q.empty() ) { printf("EMPTY QUEUE!/n"); continue; } tmp = q.top(); q.pop(); printf("%s %d/n",tmp.str,tmp.mes); } } return 0; }  

    最新回复(0)