hdoj1873 看病要排队
来源:互联网 发布:apache服务启动失败 编辑:程序博客网 时间:2024/05/16 14:25
题目:
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
- hdoj1873 看病要排队
- hdoj1873看病要排队(优先队列)
- hdu1873 看病要排队
- hdu1873 看病要排队
- hdu1873(看病要排队)
- hdu1873 看病要排队
- HDU1873 看病要排队
- hdu1873看病要排队
- HDU1873---看病要排队
- 看病要排队
- hdoj-看病要排队
- 1873 看病要排队
- hdu1873~看病要排队~
- 看病要排队
- 看病要排队
- 看病要排队
- 1873 看病要排队
- 看病要排队
- Netty系列之Netty编解码框架分析
- Github上Stars最多的53个深度学习项目,TensorFlow遥遥领先(转)
- 欧拉函数模板
- 学好spring系列之实际开发初使用
- POJ 2689 Prime Distance 素数筛选 -
- hdoj1873 看病要排队
- poj 2505 A multiplication game
- c#枚举类型
- Particle for alexa smart home skill (1)
- MVC 视图 视图引擎 语法及渲染输出
- Bnu4078距离判定
- canvas-js贝塞尔曲线代码在线生成工具
- openstack ice版文档horizon整理(页面resize)
- 【Codeforces 526F】【JZOJ4687】奇袭