hdoj1873 看病要排队

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=1873
这道题就是可以用优先队列的题,每次按优先级从大到小维护,不同的医生对应不同的队列。

编号可以从输入时就可以确定,并逐渐递增。

但有一个问题:优先级相同时,需要比较编号,先到者(小号在前)。

参考代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
const int N = 2000+10;
int n;
struct node {
    int ID;//ID号;
    int prior;//优先级;
};
node heap1[N],heap2[N],heap3[N];
int sz1,sz2,sz3;
void init() {
    memset(heap1,0,sizeof(heap1));
    memset(heap2,0,sizeof(heap2));
    memset(heap3,0,sizeof(heap3));
    sz1 = sz2 = sz3 = 0;
}
void push(node heap[],int b,int id,int &sz) {
    int i = sz++;
    while (i > 0) {
        int p = (i - 1) / 2;
        if (heap[p].prior > b || (heap[p].prior == b && heap[p].ID < id)) break;
        heap[i].prior = heap[p].prior;
        heap[i].ID = heap[p].ID;
        i = p;
    }
    heap[i].prior = b;
    heap[i].ID = id;
}
void pop(node heap[],int &sz) {
    int x = heap[--sz].prior;
    int id = heap[sz].ID;
    int i = 0;
    while (i * 2 + 1 < sz) {
        int a = i * 2 + 1;
        int b = i * 2 + 2;
        if (b < sz && ((heap[a].prior < heap[b].prior) || (heap[a].prior == heap[b].prior && heap[a].ID > heap[b].ID))) {
            a = b;
        }
        if (heap[a].prior < x || (heap[a].prior == x && heap[a].ID > id)) break;
        heap[i].prior = heap[a].prior;
        heap[i].ID = heap[a].ID;
        i = a;
    }
    heap[i].prior = x;
    heap[i].ID = id;
}
int main() {
    while (scanf("%d", &n) != EOF) {
        string s;
        int a,b;
        int id = 1;
        init();
        for (int i = 1;i <= n;++i) {
            cin >> s;
            if (s == "IN") {
                scanf("%d%d", &a, &b);
                if (a == 1) {
                    push(heap1,b,id,sz1);
                    //cout << sz1 << endl;
                    id++;
                }
                if (a == 2) {
                    push(heap2,b,id,sz2);
                    id++;
                }       
                if (a == 3) {
                    push(heap3,b,id,sz3);
                    id++;
                }               
            }
            else {
                scanf("%d", &a);
                if (a == 1) {
                    if (sz1 == 0) printf("EMPTY\n");
                    else {
                        printf("%d\n", heap1[0].ID);
                        pop(heap1,sz1);
                    }
                }
                if (a == 2) {
                    if (sz2 == 0) printf("EMPTY\n");
                    else {
                        printf("%d\n", heap2[0].ID);
                        pop(heap2,sz2);
                    }
                }               
                if (a == 3) {
                    if (sz3 == 0) printf("EMPTY\n");
                    else {
                        printf("%d\n", heap3[0].ID);
                        pop(heap3,sz3);
                    }
                }               
            }
        }
    }
    return 0;
}

当然,这道题也可以用标准库解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值