题意:模拟患者做手术。

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


代码:(Accepted,0.000s)

//UVa212 - Use of Hospital Facilities
//Accepted 0.000s
//#define _XIENAOBAN_
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<utility>
#include<string>
#include<vector>
#include<queue>
#define ipt cin
#define opt cout//just for convenience and appearance, because size("cout"+" ")=5, which is longer than a tab(which is 4)
#define time_convert(t) setw(2) << (t / 60) << ':' << setfill('0') << setw(2) << (t % 60) << setfill(' ')
using namespace std; struct ROOM {
int id, at;
ROOM() {}
ROOM(int a, int b) :id(a), at(b) {}
bool operator <(const ROOM& that) const {
if (at != that.at) return at > that.at;
return id > that.id;
}
};
struct PATIENT {
string name;
int id, room, bed, top, tre, t1, t2, t3, t4;
}; int Nop, Nre, Ts, Te, Mtr, Mop, Mre, Npa; int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 66666)
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif ios::sync_with_stdio(false);
while (ipt >> Nop) {
ipt >> Nre >> Ts >> Mtr >> Mop >> Mre >> Npa;
Te = 0, Ts *= 60;
vector<int> Top(Nop + 1, 0), Tre(Nre + 1, 0);
vector<PATIENT> Info(Npa + 1);
vector<int> Bre(Nre + 1, Ts);
priority_queue<ROOM> Rop;
for (int i(1);i <= Nop;++i) Rop.push(ROOM(i, Ts)); //For all the operating rooms
for (int N(1);N <= Npa;++N) {
auto& now(Info[N]);
now.id = N;
ROOM op(Rop.top());
Rop.pop();
ipt >> now.name >> now.top >> now.tre;
now.t1 = op.at;
now.t2 = now.t1 + now.top;
now.t3 = now.t2 + Mtr;
now.t4 = now.t3 + now.tre;
now.room = op.id;
op.at = now.t2 + Mop;
Top[op.id] += now.top;
Rop.push(op);
}
sort(Info.begin() + 1, Info.end(), [](PATIENT& a, PATIENT& b)->bool
{if (a.t2 != b.t2) return a.t2 < b.t2;return a.room < b.room;});//Sort by t2 //For all the recovery rooms
for (int N(1);N <= Npa;++N) {
auto& now(Info[N]);
int re;
for (re = 1;re <= Nre;++re)
if (Bre[re] <= now.t2) break;
now.bed = re;
Bre[re] = now.t4 + Mre;
Tre[re] += now.tre;
if (Te < now.t4) Te = now.t4;
}
sort(Info.begin() + 1, Info.end(), [](PATIENT& a, PATIENT& b) {return a.id < b.id;});//Sort by id //Output
opt << " Patient Operating Room Recovery Room\n"
<< " # Name Room# Begin End Bed# Begin End\n"
<< " ------------------------------------------------------\n";
for (int N(1);N <= Npa;++N) {
auto& now(Info[N]);
opt << setw(2) << N << " " << left << setw(10) << now.name << right << setw(2)
<< now.room << " " << time_convert(now.t1) << " " << time_convert(now.t2) << " " << setw(2)
<< now.bed << " " << time_convert(now.t3) << " " << time_convert(now.t4) << '\n';
}
opt << '\n'
<< "Facility Utilization\n"
<< "Type # Minutes % Used\n"
<< "-------------------------\n";
for (int N(1);N <= Nop;++N) {
opt << "Room " << setw(2) << N << setw(8) << Top[N]
<< setw(8) << fixed << setprecision(2) << (Top[N] * 100.0 / (float)(Te - Ts)) << '\n';
}
for (int N(1);N <= Nre;++N) {
opt << "Bed " << setw(2) << N << setw(8) << Tre[N]
<< setw(8) << fixed << setprecision(2) << (Tre[N] * 100.0 / (float)(Te - Ts)) << '\n';
}
cout << endl;
}
return 0;
}

分析:终于是本章节的最后一题啦!书上说很难,感觉其实不难啊,就是麻烦了点,写了好久。就是死在了一句话上“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. 请一定记得升级java虚拟机

    对于吃货出身又需要保持体重的我,出门一定要带男票,因为这样就可以把见到的好吃的都买给他吃,就当是自己吃了[汗].偶尔做梦还是会梦到自己一个角落里偷吃东西,听到有脚步声,抬起头,大哭起来:“我饿了.”  ...

  2. 微信web开发者工具使用

    1.首先启动微信web开发者工具, 2.启动之后,点击移动端调试, 3.选择普通调试,然后,将手机和电脑置于同一个网段之中,可以通过电脑发出一个wifi,让手机连入就行 4.如果,电脑室台式机的话,没 ...

  3. 深入探究stm32GPIO口模式(类比51)

    关于STM32GPIO口的8种工作模式,我们先引出一些问题? STM32GPIO口如果既要输入又要输出怎么办? 1.浮空输入模式 上图红色的表示便是浮空输入的过程,外部输入时0读出的就是0,外部输入时 ...

  4. 手机自动化测试:appium源码分析之bootstrap五

    手机自动化测试:appium源码分析之bootstrap五   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试 ...

  5. [转]使用sklearn进行集成学习——理论

    转:http://www.cnblogs.com/jasonfreak/p/5657196.html 目录 1 前言2 集成学习是什么?3 偏差和方差 3.1 模型的偏差和方差是什么? 3.2 bag ...

  6. 【Scala】Scala之Packaging and Imports

    一.前言 前面介绍了Scala的Object,由于中间论文的时间耽误了些时间,接着继续学习Scala的Packaging和Imports 二.Packaging and Imports Scala的包 ...

  7. HTML5技术实现Web图形图像处理——WebPhotoshop精简版

    WebPhotoshop精简版是利用HTML5技术在Web上实现对图形图像的处理,构建易维护.易共享.易于拓展.实时性的Web图形图像处理平台. 精简版功能包括:图形绘制.图像处理.图像操作.完整版包 ...

  8. .exe简单的更新软件demo

    百度网盘源码加文件:http://pan.baidu.com/s/1qYe2Vgg 功能:通过网站更新用户的软件,需要联网,也可以通过本地网站更新局域网用户软件. 根本实现:1.一个网站(本地就可以) ...

  9. JSSDK微信自定义分享

    背景:15年之前的微信分享只需要加入一段js就可以实现.后来微信官方全部禁止了.现在的微信分享全部得使用jssdk. 一.分享功能: 在微信内(必须在微信里)打开网站页面,分享给朋友或者分享到朋友圈时 ...

  10. Python自学笔记——matplotlib极坐标.md

    一.极坐标 在平面内取一个定点O,叫极点,引一条射线Ox,叫做极轴,再选定一个长度单位和角度的正方向(通常取逆时针方向).对于平面内任何一点M,用ρ表示线段OM的长度(有时也用r表示),θ表示从Ox到 ...