hdu 1873 看病要排队(优先队列)

来源:互联网 发布:淘宝假冒伪劣产品投诉 编辑:程序博客网 时间:2024/05/18 07:57

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1873

题       意:输入数据包含多组测试,请处理到文件结束。
                             每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
                  接下来有N行分别表示发生的事件。
                          一共有两种事件:
                                       1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
                                       2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)                     

                       对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
                      诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。
思       路:以B为优先关键字,建立一个STL的优先队列进行模拟。
代码如下:
#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <queue>#include <algorithm>struct node{    int id,num;    bool operator < ( const node &a ) const    {        if( a.num != num ) return a.num > num;        return a.id < id;    }};int main(){    int n;    while( scanf ( "%d", &n ) != EOF )    {        priority_queue<node> q1,q2,q3;        int k = 1;        while( n-- )        {            char str[5];            scanf ( "%s", str );            if( str[0] == 'I' )            {                int x, y;                scanf ( "%d %d", &x, &y );                node s;                s.num=y;                s.id=k++;                if( x == 1 ) q1.push(s);                if( x == 2 ) q2.push(s);                if( x == 3 ) q3.push(s);            }            else if( str[0] == 'O' )            {                int x;                scanf ( "%d", &x );                if( x == 1 )                {                    if(!q1.empty())                    {                        printf("%d\n",q1.top().id);                        q1.pop();                    }                    else printf("EMPTY\n");                }                else if( x == 2 )                {                    if(!q2.empty())                    {                        printf("%d\n",q2.top().id);                        q2.pop();                    }                    else printf("EMPTY\n");                }                else if( x == 3 )                {                    if(!q3.empty())                    {                        printf("%d\n",q3.top().id);                        q3.pop();                    }                    else printf("EMPTY\n");                }            }        }    }    return 0;}


 
 

 

0 0
原创粉丝点击