UVA 212 Use of Hospital Facilities
题目链接:https://vjudge.net/problem/UVA-212
题意摘自《算法禁赛入门经典》
题目大意
分析
- 多组数据,数据之间空一行,每组数据表间空一行。
- 0 个病人的情况。
- 手术结束时间相同的,手术室编号小的先安排。
- 手术完成后应当立即选择恢复室并移动。
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
#define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
#define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,0x3f,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size() - ];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef unsigned int uI;
typedef long long LL;
typedef unsigned long long uLL;
typedef vector< int > VI;
typedef vector< bool > VB;
typedef vector< char > VC;
typedef vector< double > VD;
typedef vector< string > VS;
typedef vector< LL > VL;
typedef vector< VI > VVI;
typedef vector< VB > VVB;
typedef vector< VS > VVS;
typedef vector< VL > VVL;
typedef vector< VVI > VVVI;
typedef vector< VVL > VVVL;
typedef pair< int, int > PII;
typedef pair< LL, LL > PLL;
typedef pair< int, string > PIS;
typedef pair< string, int > PSI;
typedef pair< string, string > PSS;
typedef pair< double, double > PDD;
typedef vector< PII > VPII;
typedef vector< PLL > VPLL;
typedef vector< VPII > VVPII;
typedef vector< VPLL > VVPLL;
typedef vector< VS > VVS;
typedef map< int, int > MII;
typedef unordered_map< int, int > uMII;
typedef map< LL, LL > MLL;
typedef map< string, int > MSI;
typedef map< int, string > MIS;
typedef multiset< int > mSI;
typedef set< int > SI;
typedef stack< int > SKI;
typedef deque< int > DQI;
typedef queue< int > QI;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e4 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Time {
int val = ; // 总的分钟数 bool operator< (const Time &x) const {
return val < x.val;
} bool operator== (const Time &x) const {
return val == x.val;
} bool operator> (const Time &x) const {
return val > x.val;
} Time operator+ (const int &k) const {
Time ret;
ret.val = val + k;
return ret;
} Time operator+ (const Time &x) const {
Time ret;
ret.val = val + x.val;
return ret;
} string getString() {
string ret;
ret = toString(val / ) + ":";
if((val % ) / == ) ret += "";
ret += toString(val % );
return ret;
}
}; struct Patient {
int id; // 病人在,名册上的 id
string name; // 病人名字
int surgeryTime; // 病人手术时间
int recoveryTime; // 病人恢复时间 int surgeryRoomId; // 所进行手术的手术室 id
Time surgeryBeginTime; // 手术开始时间
Time surgeryEndTime; // 手术结束时间 int recoveryRoomId; // 所进行恢复的恢复室 id
Time recoveryBeginTime; // 恢复开始时间
Time recoveryEndTime; // 恢复结束时间 Patient(int x) : id(x) {}
}; istream& operator >> (istream& in, Patient& x){
in >> x.name >> x.surgeryTime >> x.recoveryTime;
return in;
} ostream& operator << (ostream& out, Patient& x){
printf("%2d %-8s %2d %5s %5s %2d %5s %5s", x.id, x.name.c_str(), x.surgeryRoomId, x.surgeryBeginTime.getString().c_str(), x.surgeryEndTime.getString().c_str(), x.recoveryRoomId, x.recoveryBeginTime.getString().c_str(), x.recoveryEndTime.getString().c_str());
return out;
} struct Facility {
int id; // 设施 id
string type; // 设施类型
int timeUsed; // 设施被使用的时间
double usedPercent; // 使用时间百分比 Facility(int x, string y) {
id = x;
type = y;
timeUsed = ;
usedPercent = ;
}
}; ostream& operator << (ostream& out, const Facility& x) {
printf("%-4s %2d %7d %6.2f", x.type.c_str(), x.id, x.timeUsed, x.usedPercent);
return out;
} struct Event {
// 1 代表患者可进行手术事件
// 2 代表患者可进行恢复事件
// 3 代表手术室空闲事件
// 4 代表恢复室空闲事件
int type;
int idx; // 对应数组下表
Time time; // 事件可进行时间 Event(int x, int y, Time z) {
type = x;
idx = y;
time = z;
} bool operator< (const Event &x) const {
return time > x.time || time == x.time && idx > x.idx;
}
}; int N; // 手术室数量,小于等于 10
vector< Facility > rooms; // 手术室房间数组
int M; // 恢复室数量,小于等于 30
vector< Facility > beds; // 恢复室床数组
Time startTime; // 一天中医院的开始营业时间,整点,24小时格式
Time endTime; // 所有手术的结束时间
int totalTime; // 手术总持续时间
int tranTime; // 手术室转移恢复室时间(分钟)
int surgeryResetTime; // 手术室准备时间(分钟)
int recoveryResetTime; // 恢复室准备时间(分钟)
vector< Patient > roster; // 病人名册
int patientNum; // 病人人数
int tmp; struct RoomCmp {
bool operator() (int x, int y) {
return rooms[x].id > rooms[y].id;
}
}; struct BedCmp {
bool operator() (int x, int y) {
return beds[x].id > beds[y].id;
}
}; struct RecoveryComp {
bool operator() (int x, int y) {
if(roster[x].surgeryEndTime == roster[y].surgeryEndTime) return roster[x].surgeryRoomId > roster[y].surgeryRoomId;
return roster[x].surgeryEndTime > roster[y].surgeryEndTime;
}
}; priority_queue< Event > Q; // 活动队列
QI surgeryQ; // 等待手术队列
priority_queue< int, VI, RecoveryComp > recoveryQ; // 等待恢复队列
priority_queue< int, VI, RoomCmp > roomQ; // 手术室空闲队列
priority_queue< int, VI, BedCmp > bedQ; // 恢复室空闲队列 void init() {
rooms.clear();
beds.clear();
endTime = startTime;
roster.clear(); while(!Q.empty()) Q.pop();
while(!surgeryQ.empty()) surgeryQ.pop();
while(!recoveryQ.empty()) recoveryQ.pop();
while(!roomQ.empty()) roomQ.pop();
while(!bedQ.empty()) bedQ.pop();
} bool readData() {
if(!(cin >> N >> M >> tmp >> tranTime >> surgeryResetTime >> recoveryResetTime >> patientNum)) return false;
startTime.val = tmp * ; init(); Rep(i, N) rooms.PB(Facility(i + , "Room"));
Rep(i, M) beds.PB(Facility(i + , "Bed")); Rep(i, patientNum) {
roster.PB(Patient(i + ));
cin >> roster[i];
} return true;
} void printSheet() {
printf(" Patient Operating Room Recovery Room\n");
printf(" # Name Room# Begin End Bed# Begin End\n");
printf(" ------------------------------------------------------\n");
Rep(i, patientNum) cout << roster[i] << endl; cout << endl; printf("Facility Utilization\n");
printf("Type # Minutes % Used\n");
printf("-------------------------\n");
Rep(i, N) {
if(totalTime) rooms[i].usedPercent = 100.0 * rooms[i].timeUsed / totalTime;
cout << rooms[i] << endl;
}
Rep(i, M) {
if(totalTime) beds[i].usedPercent = 100.0 * beds[i].timeUsed / totalTime;
cout << beds[i] << endl;
}
} void solve() {
Time nowTime;
Rep(i, patientNum) Q.push(Event(, i, startTime));
Rep(i, N) Q.push(Event(, i, startTime));
Rep(i, M) Q.push(Event(, i, startTime)); while(!Q.empty()) {
nowTime = Q.top().time; while(!Q.empty() && nowTime == Q.top().time) { // 把这个时间到到达的活动全取出来
Event e = Q.top(); Q.pop(); switch(e.type) {
case :{
surgeryQ.push(e.idx);
break;
}
case :{
recoveryQ.push(e.idx);
break;
}
case :{
roomQ.push(e.idx);
break;
}
case :{
bedQ.push(e.idx);
break;
}
default:{
assert(false);
}
}
} // 如果有人等待做手术并且有手术室可用,就分配手术室
while(!surgeryQ.empty() && !roomQ.empty()) {
int pid = surgeryQ.front(); surgeryQ.pop();
int rid = roomQ.top(); roomQ.pop();
Patient &p = roster[pid];
Facility &f = rooms[rid]; p.surgeryRoomId = f.id;
p.surgeryBeginTime = nowTime;
p.surgeryEndTime = p.surgeryBeginTime + p.surgeryTime; f.timeUsed += p.surgeryTime; Q.push(Event(, pid, p.surgeryEndTime));
Q.push(Event(, rid, p.surgeryEndTime + surgeryResetTime));
} // 如果有人等待恢复并且有恢复室可用,就分配恢复室
while(!recoveryQ.empty() && !bedQ.empty()) {
int pid = recoveryQ.top(); recoveryQ.pop();
int bid = bedQ.top(); bedQ.pop();
Patient &p = roster[pid];
Facility &f = beds[bid]; p.recoveryRoomId = f.id;
p.recoveryBeginTime = nowTime + tranTime;
p.recoveryEndTime = p.recoveryBeginTime + p.recoveryTime; f.timeUsed += p.recoveryTime; endTime = max(endTime, p.recoveryEndTime); Q.push(Event(, bid, p.recoveryEndTime + recoveryResetTime));
}
}
} int main() {
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
while(readData()) {
solve();
totalTime = endTime.val - startTime.val;
printSheet();
cout << endl;
}
return ;
}
UVA 212 Use of Hospital Facilities的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities
题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...
- bash5.0参考手册
Bash Reference Manual a.summary-letter { text-decoration: none } blockquote.indentedblock { margin-r ...
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- 紫书 习题 11-10 UVa 12264 (二分答案+最大流)
书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- 1Z0-053 争议题目解析212
1Z0-053 争议题目解析212 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 212.Note the following parameters settings in you ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
随机推荐
- MVC 入门
MVC是什么? MVC是一个框架模式,它用于把应用程序的输入.处理和输出进行强制性的分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.最典型的MVC就是JSP+S ...
- charles抓包看性能数据
1.优化某个接口或加载速度(H5加载速度慢) 抓包看Overview ①看Duration,就是接口的加载时间 ②看Latency,就是延时一端传播到另一端所花费的时间:一般和网络有关:可以综合Dur ...
- 安装SSH2拓展 PHP上传文件到远程服务器
情景:客户端上传图片到服务器A,服务器A同步上传至另外一个静态资源服务器B 环境:php7 linux(ubuntu) 安装php的ssh2扩展 -dev sudo apt-get install p ...
- upc组队赛16 Melody【签到水】
Melody 题目描述 YellowStar is versatile. One day he writes a melody A = [A1, ..., AN ], and he has a sta ...
- git使用记录二: 给文件重命名的简单方法
git使用记录三: 给文件重命名的简单方法 git mv file_name_old file_name_new mv: 文件命名 file_name_old : 文件当前的名字 file_name_ ...
- 转:父类私有变量是否被子类继承详细解说(答案:内存中存在,但sun公司定义为不继承)
应作者要求,本处提供一个连接,表示对原作者版权尊重. https://blog.csdn.net/mr_duantao/article/details/50966471
- List<Map>去重并合并数量
今天在查询出的sql中,出现了部门名称和部门ID有重合的数据!当然这样在页面上展示也会容易一起误导!查询出的数据结构如下图 希望根据deptid中 >最后一个节点id把deptname 合并成& ...
- 44-python基础-python3-字符串-常用字符串方法(二)-isalpha()-isalnum()-isdigit()-isspace()-istitle()
3-isX 字符串方法 序号 方法 条件 返回结果1 返回结果2 1 isalpha() 如果字符串只包含字母,并且非空; True False 2 isalnum() 如果字符串只包含字母和数字 ...
- 2018-2-13-win10-uwp-从-Unity-创建
title author date CreateTime categories win10 uwp 从 Unity 创建 lindexi 2018-2-13 17:23:3 +0800 2018-2- ...
- Xfce4: 显示/隐藏 休眠/混合睡眠/挂起 按钮
可以在xfconf配置项中创建如下属性控制: xfconf-query -c xfce4-session -np '/shutdown/ShowHibernate' -t 'bool' -s 'fal ...