题意:模拟患者做手术。

其条件为:医院有Nop个手术室、准备手术室要Mop分钟,另有Nre个恢复用的床、准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟。现在医院早上开张了,给你一张患者的表,有Npa个患者等着做手术,每个患者的的信息有:名字、做手术需要的时间、恢复需要的时间。只要有空的手术室位就安排患者进去,优先安排门牌号低的。若多人同时竞争,输入列表靠前的先进。进入恢复室的优先顺序是,也是优先安排床号靠前的床,同时做完手术的人按照手术室的门牌号小的 (很奇葩的设定)优先。


代码:(Accepted,0.000s)

  1. //UVa212 - Use of Hospital Facilities
  2. //Accepted 0.000s
  3. //#define _XIENAOBAN_
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<iomanip>
  7. #include<utility>
  8. #include<string>
  9. #include<vector>
  10. #include<queue>
  11. #define ipt cin
  12. #define opt cout//just for convenience and appearance, because size("cout"+" ")=5, which is longer than a tab(which is 4)
  13. #define time_convert(t) setw(2) << (t / 60) << ':' << setfill('0') << setw(2) << (t % 60) << setfill(' ')
  14. using namespace std;
  15. struct ROOM {
  16. int id, at;
  17. ROOM() {}
  18. ROOM(int a, int b) :id(a), at(b) {}
  19. bool operator <(const ROOM& that) const {
  20. if (at != that.at) return at > that.at;
  21. return id > that.id;
  22. }
  23. };
  24. struct PATIENT {
  25. string name;
  26. int id, room, bed, top, tre, t1, t2, t3, t4;
  27. };
  28. int Nop, Nre, Ts, Te, Mtr, Mop, Mre, Npa;
  29. int main()
  30. {
  31. #ifdef _XIENAOBAN_
  32. #define gets(T) gets_s(T, 66666)
  33. freopen("in.txt", "r", stdin);
  34. freopen("out.txt", "w", stdout);
  35. #endif
  36. ios::sync_with_stdio(false);
  37. while (ipt >> Nop) {
  38. ipt >> Nre >> Ts >> Mtr >> Mop >> Mre >> Npa;
  39. Te = 0, Ts *= 60;
  40. vector<int> Top(Nop + 1, 0), Tre(Nre + 1, 0);
  41. vector<PATIENT> Info(Npa + 1);
  42. vector<int> Bre(Nre + 1, Ts);
  43. priority_queue<ROOM> Rop;
  44. for (int i(1);i <= Nop;++i) Rop.push(ROOM(i, Ts));
  45. //For all the operating rooms
  46. for (int N(1);N <= Npa;++N) {
  47. auto& now(Info[N]);
  48. now.id = N;
  49. ROOM op(Rop.top());
  50. Rop.pop();
  51. ipt >> now.name >> now.top >> now.tre;
  52. now.t1 = op.at;
  53. now.t2 = now.t1 + now.top;
  54. now.t3 = now.t2 + Mtr;
  55. now.t4 = now.t3 + now.tre;
  56. now.room = op.id;
  57. op.at = now.t2 + Mop;
  58. Top[op.id] += now.top;
  59. Rop.push(op);
  60. }
  61. sort(Info.begin() + 1, Info.end(), [](PATIENT& a, PATIENT& b)->bool
  62. {if (a.t2 != b.t2) return a.t2 < b.t2;return a.room < b.room;});//Sort by t2
  63. //For all the recovery rooms
  64. for (int N(1);N <= Npa;++N) {
  65. auto& now(Info[N]);
  66. int re;
  67. for (re = 1;re <= Nre;++re)
  68. if (Bre[re] <= now.t2) break;
  69. now.bed = re;
  70. Bre[re] = now.t4 + Mre;
  71. Tre[re] += now.tre;
  72. if (Te < now.t4) Te = now.t4;
  73. }
  74. sort(Info.begin() + 1, Info.end(), [](PATIENT& a, PATIENT& b) {return a.id < b.id;});//Sort by id
  75. //Output
  76. opt << " Patient Operating Room Recovery Room\n"
  77. << " # Name Room# Begin End Bed# Begin End\n"
  78. << " ------------------------------------------------------\n";
  79. for (int N(1);N <= Npa;++N) {
  80. auto& now(Info[N]);
  81. opt << setw(2) << N << " " << left << setw(10) << now.name << right << setw(2)
  82. << now.room << " " << time_convert(now.t1) << " " << time_convert(now.t2) << " " << setw(2)
  83. << now.bed << " " << time_convert(now.t3) << " " << time_convert(now.t4) << '\n';
  84. }
  85. opt << '\n'
  86. << "Facility Utilization\n"
  87. << "Type # Minutes % Used\n"
  88. << "-------------------------\n";
  89. for (int N(1);N <= Nop;++N) {
  90. opt << "Room " << setw(2) << N << setw(8) << Top[N]
  91. << setw(8) << fixed << setprecision(2) << (Top[N] * 100.0 / (float)(Te - Ts)) << '\n';
  92. }
  93. for (int N(1);N <= Nre;++N) {
  94. opt << "Bed " << setw(2) << N << setw(8) << Tre[N]
  95. << setw(8) << fixed << setprecision(2) << (Tre[N] * 100.0 / (float)(Te - Ts)) << '\n';
  96. }
  97. cout << endl;
  98. }
  99. return 0;
  100. }

分析:终于是本章节的最后一题啦!书上说很难,感觉其实不难啊,就是麻烦了点,写了好久。就是死在了一句话上“If two patients emerge from surgery at the same time, the patient with the lower number will be the first assigned to a recovery room bed.”(就是上面题意里加黑的那句)我还以为指的是患者编号,即患者输入顺序。结果WA。。。udebug上没有数据,不知道问题在哪里。自己做了一套数据发现了错误,百度查了别人的排序逻辑,才发现原来那个the patient with the lower number指的是在门牌号小的手术室做手术的患者。那个number竟然指的门牌号!所以只把sort函数的lambda比较函数改了一小下就AC了。

手术室的模拟使用了优先队列,空一个房间就来一个人,但是恢复床位不能用优先队列。因为恢复床位进入可使用状态最早的不一定是床号最小的,即当有人要转移到床位时可使用床位可能不止一个,和他做完手术的时间有关系。

附:测试数据

10 30 01 16 15 1 30

Jones

90 140

Smith

10 200

Thompson

60 75

Albright

40 82

Poucher

33 209

Comer

10 201

Perry

3 188

Page

111 120

Roggio

69 100

Brigham

42 79

Nute

22 71

Young

38 50

Bush

26 40

Cates

120 32

Johnson

10 2

Jones

28 140

Smith

120 200

Thompson

23 75

Albright

19 82

Poucher

133 209

Comer

74 101

Perry

93 188

Page

111 223

Roggio

69 122

Brigham

42 79

Nute

22 71

Young

38 140

Bush

26 121

Cates

120 248

Johnson

10 50

[刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...

  2. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  3. [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A

    题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...

  4. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...

  5. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation

    题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...

  6. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  7. [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536

    这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...

  8. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  9. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...

随机推荐

  1. pyhton中的Queue(队列)

    什么是队列? 队列就像是水管子,先进先出,与之相对应的是栈,后进先出. 队列是线程安全的,队列自身有机制可以实现:在同一时刻只有一个线程在对队列进行操作. 存数据,取数据 import Queue q ...

  2. Windows:将cmd命令行添加到右键中方法

    win10中将命令行cmd添加到右键的方法 Windows cmd 右键 win10 命令行 最近在学python,所以会用到很多库文件,但是有的库文件需要下载whl文件再通过cmd进行安装,所以每次 ...

  3. Myeclipse Java项目转换成Maven项目

    1.在Eclipse中Java项目转换成Maven项目可以在项目右键-->configure-->Convert Plug-in projects..  就可以.而在myeclipse中项 ...

  4. 关于JDEV的连接问题

    在JDev中有两个连接数据哭库的地方,双击项目名称,里面的Business Components里面的Connection里面的链接,这个链接是Run页面时候的链接 第二个链接在Oracle Appl ...

  5. 自动化监控利器-Zabbix深入配置和使用

    1.  配置流程 Zabbix完整的监控配置流程可以简单描述为: Host groups(主机组)→Hosts(主机)→Applications(监控项组)→Items(监控项)→Triggers(触 ...

  6. C语言常见错误笔记

    1. 职业化的程序员起码要具备两点: 1)基本的软件技能 2)不犯低级的错误 2. 修改函数的形参是没用的,函数本身占用的存储单元在堆栈中分配,入口参数的值会在函数入口处拷贝到堆栈中,一旦函数返回,其 ...

  7. State模式学习笔记

    选用了一个假设需要用户验证的例子进行State模式学习,这个例子并不恰当.无所谓了,只要能学习到其中的内容即可. 适用性: 1,一个对象的行为取决于他的状态,并且它必须在运行时刻依据状态改变他的行为. ...

  8. 解决Mybatis连接Sql server 出现 Cannot load JDBC driver class 'com.mysql.jdbc.Driver '的问题

    tomcat启动的时候没有错误,但是进行数据库操作就会有错误. 在网上找了很久  好不容易找到解决方法 转自 http://blog.csdn.net/ro_bot/article/details/5 ...

  9. [Paxos] Paxos Made Simple 读后感

    Paxos 由著名图灵奖获得者Leslie Lamport提出,该算法是分布式一致性算法中的奠基之作,今天初读此文仅将相关学习心得予以记录. 1.Paxos 是什么?主要用来解决什么问题? Paxos ...

  10. 倒排索引的AND操作

    这是一道来自百度的面试题.倒排索引的AND操作. 倒排索引是以关键词作为索引项来索引文档的一种机制,如图中Brutus.Calpurnia.Caesar为关键词,2.4.8等等为文档ID. 现在有一个 ...