构造事件队列,依次处理即可。
注意:如果两人同时做完手术,则手术室序号小的人优先进入恢复室

#include<bits/stdc++.h>
using namespace std;
struct Event{int time,id,type;Event(int t,int id,int type):time(t),id(id),type(type){}bool operator<(const Event&e)const {return time>e.time;}
};
enum eventType{opFree=0,opPre=1,reFree=3,rePre=4
};
struct Room
{int pat,minutes;void init(){pat=-1;minutes=0;}
};
struct Patient{string name;int surgeryTime,recoveryTime,opRoomId,opBeginTime,opEndTime,reRoomId,reBeginTime,reEndTIme;
};
priority_queue<Event>em;
int nOp,nRe,T0,tTrans,tPreOp,tPreRe,nPat,allTime;
Room opRooms[10+1],reRooms[30+1];
Patient pats[100+1];
struct patComp
{bool operator()(int i1,int i2){const Patient& p1=pats[i1];const Patient&p2=pats[i2];assert(p1.opRoomId!=-1&&p2.opRoomId!=-1);return p1.opRoomId<p2.opRoomId;}
};
set<int>opQueue,freeOpRooms,freeReRooms;
set<int,patComp>reQueue;
typedef set<int>::iterator siit;
void writeTime(char*buf,int time){int h=time/60+T0,m=time%60;sprintf(buf,"%2d:%02d",h,m);
}
ostream&operator<<(ostream&os,const Patient& p)
{char buf[18];sprintf(buf,"  %-10s%2d   ",p.name.c_str(),p.opRoomId+1);os<<buf;writeTime(buf,p.opBeginTime); os<<buf<<"   ";writeTime(buf,p.opEndTime);os<<buf;sprintf(buf,"%7d",p.reRoomId+1);os<<buf<<"   ";writeTime(buf,p.reBeginTime);os<<buf<<"   ";writeTime(buf,p.reEndTIme);os<<buf;return os;
}
ostream& operator<<(ostream&os,const Room&r)
{double p=r.minutes*100;p/=allTime;char buf[64];sprintf(buf,"%8d  %6.2lf",r.minutes,p);return os<<buf;
}
void solve(){int time=em.top().time;while(!em.empty()&&em.top().time==time){Event e=em.top();em.pop();int pid;switch(e.type){case opFree:assert(!freeOpRooms.count(e.id));freeOpRooms.insert(e.id);assert(opRooms[e.id].pat==-1);break;case opPre:assert(!freeOpRooms.count(e.id));pid=opRooms[e.id].pat;assert(pid!=-1);reQueue.insert(pid);opRooms[e.id].pat=-1;em.push(Event(time+tPreOp,e.id,opFree));break;case reFree:assert(!freeReRooms.count(e.id));assert(reRooms[e.id].pat==-1);freeReRooms.insert(e.id);break;case rePre:assert(!freeReRooms.count(e.id));assert(reRooms[e.id].pat!=-1);reRooms[e.id].pat=-1;em.push(Event(time+tPreRe,e.id,reFree));break;default:assert(false);}}int opSz=min(opQueue.size(),freeOpRooms.size());for(int i=0;i<opSz;++i){int pid=*(opQueue.begin());opQueue.erase(opQueue.begin());int rid=*(freeOpRooms.begin());freeOpRooms.erase(freeOpRooms.begin());Room&r =opRooms[rid];r.pat=pid;Patient&p=pats[pid];p.opRoomId=rid;p.opBeginTime=time;p.opEndTime=time+p.surgeryTime;r.minutes+=p.surgeryTime;em.push(Event(p.opEndTime,rid,opPre));}int reSz=min(reQueue.size(),freeReRooms.size());for(int i=0;i<reSz;++i){int pid=*(reQueue.begin());reQueue.erase(reQueue.begin());int rid=*(freeReRooms.begin());freeReRooms.erase(freeReRooms.begin());Room&r=reRooms[rid];r.pat=pid;Patient&p=pats[pid];p.reRoomId=rid;p.reBeginTime=time+tTrans;p.reEndTIme=p.reBeginTime+p.recoveryTime;r.minutes+=p.recoveryTime;em.push(Event(p.reEndTIme,rid,rePre));allTime=max(allTime,p.reEndTIme);}
}
int main()
{//freopen("../input.txt","r",stdin);//freopen("../output.txt","w",stdout);while(cin>>nOp){assert(opQueue.empty());assert(reQueue.empty());assert(em.empty());freeOpRooms.clear();freeReRooms.clear();allTime=0;cin>>nRe>>T0>>tTrans>>tPreOp>>tPreRe>>nPat;for(int i=0;i<nOp;++i){em.push(Event(0,i,opFree));opRooms[i].init();}for(int i=0;i<nRe;++i){em.push(Event(0,i,reFree));reRooms[i].init();}for(int i=0;i<nPat;++i){Patient&p=pats[i];p.opRoomId=-1;p.reRoomId=-1;cin>>p.name>>p.surgeryTime>>p.recoveryTime;opQueue.insert(i);}while(!em.empty()){solve();}cout<<" Patient          Operating Room          Recovery Room\n";cout<<" #  Name     Room#  Begin   End      Bed#  Begin    End\n";cout<<" ------------------------------------------------------\n";for(int i=0;i<nPat;++i)cout<<setw(2)<<i+1<<pats[i]<<endl;cout<<endl;cout<<"Facility Utilization"<<endl;cout<<"Type  # Minutes  % Used\n";cout<<"-------------------------\n";for(int i=0;i<nOp;++i)cout<<"Room "<<setw(2)<<i+1<<opRooms[i]<<endl;for(int i=0;i<nRe;++i)cout<<"Bed  "<<setw(2)<<i+1<<reRooms[i]<<endl;cout<<endl;}
}

紫书5-16UVa212相关推荐

  1. UVa 1339,紫书P73,词频

    题目链接:https://uva.onlinejudge.org/external/13/1339.pdf 紫书P73 解题报告: #include <stdio.h> #include ...

  2. 11行代码AC——比紫书优化,例题2-3 近似计算——解题报告

    励志用更少的代码做高效的表达 题意: 计算π/4 = 1 - 1/3 + 1/5 - 1/7 + -,直到最后一项小于10^-6. 思路分析: 本题很简单,因此计算重心从解题转化为优化. 本题为重复计 ...

  3. 比紫书优化,14行代码AC——例题 5-7 丑数(Ugly Numbers,UVa 136)——解题报告

    题意: 丑数是一些因子只有2,3,5的数.数列1,2,3,4,5,6,8,9,10,12,15--写出了从小到大的前11个丑数,1属于丑数.现在请你编写程序,找出第1500个丑数是什么. 没有输入 输 ...

  4. 紫书 团队队列(队列)

    1.用两个队列来维护成员,一个队列维护队伍编号,一个队列数组维护每个队伍的成员 ps: 紫书p118 #include <bits/stdc++.h> using namespace st ...

  5. (紫书,感谢作者)第7章暴力求解法

    今天,我们谈一谈紫书上面的内容--暴力求解法 对于一道问题来说,我们是可以借助计算机运算快的特点,将所有可能的情况全部(不一定是全部)列出来,然后去寻找我们想要的答案,这就是暴力求解了,但暴力求解绝对 ...

  6. 【紫书】(UVa12096) The SetStack Computer

    突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来.那么里面用到了这一题的思想,我们就先解决这题.当然,dp必须继续做下去,这是基本功.断不得. 题意分析 这条题真 ...

  7. ACM入门攻略(紫书入门,不间断更新)

    声明:本文仅供参考,并且假定读者已经可以熟练运用C语言及其相关知识(大神请走开,谢谢配合). 目录:一.ACM入门的相关准备(书籍,OJ,编程语言,常用网站或工具)            二.入门阶段 ...

  8. 【紫书】第一章实验+问题

    紫书第1章实验 实验A1:表达式11111∗1111111111\ast1111111111∗11111的值是多少?改成6个1呢?9个1呢? 以下几个都用有符号整数%d\%d%d输出结果. 表达式11 ...

  9. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  10. UVA 1025 紫书练习题 动态规划

    好吧,做例题总是这样,莫名其妙的WA,然后莫名其妙的AC... 关键的部分紫书上已经说得很清楚了,剩下的就是确定has_train这个数组,我觉得确定这个数组也不是难事吧..把数据范围看清楚,然后看看 ...

最新文章

  1. dsp处理浮点数_关于IQMATH和浮点数在DSP的深入理解
  2. 05-数据类型、常量、变量
  3. linux下部署svn服务器
  4. 如何改变 Python 中线程的执行顺序
  5. ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
  6. 数据库中触发器、事务
  7. Docker笔记——jdk镜像制作
  8. 腾讯宣布开源 RoP:Apache Pulsar 支持原生 RocketMQ 协议
  9. 算法应用-斐波那契数列
  10. 在字节,校招刚入职就得到业界大会talk机会是一种怎样的体验?
  11. Erlang中的OTP简要
  12. 【LeetCode】【refine 1】题号:1. 两数之和
  13. ASP.NET中的图片缓存
  14. MAPinfo学习笔记
  15. JMETER录制脚本,脚本增强,参数化,作用域和执行顺序
  16. JMP软件在质量管理中的应用简述
  17. 终端使用sopcast例子
  18. 模拟退火MATLAB
  19. python 删除pdf页面_Python脚本使用pyPDF删除空白页面
  20. 前辈做的电子地图API调研,转过来…

热门文章

  1. MySQL和Navicat怎么连接
  2. 手机号正则(2020年4月15日)
  3. 一年级课程表(4月2日-4月8日)
  4. 非root用户安装或升级CUDA和CUDNN版本
  5. Windows10怎么改分辨率?Win10分辨率设置教程(最新)
  6. PX4新增自定义orb消息
  7. cnn程序流程图_画流程图的基本原则
  8. python-seleium实现珞珈在线网课辅助观看功能
  9. vscode设置鼠标滚轮滚动 翻页速度
  10. OpenWrt路由器设置万能中继及如何永久修改MAC地址