您的位置:首页 > 其它

Hospital(队列+模拟)

2015-06-22 11:06 288 查看

Hospital

Description

Have you ever played the game "Theme Hospital"? In this game, you act the manager of the hospital. Many patients are going to the hospital consecutively, and you must build enough equipment to heal the patients. If you couldn’t heal them in time, the patients
will be angry, and go out with complaint. If the hospital is complained by too many patients, it will be shut down forcibly and the game will be over.

Here is an simplified version of it.

The hospital has one reception desk and m service rooms. At the reception desk, the patients can check the current information of all the service rooms (i.e. the number of patients already in each room). The rooms are numbered from 1 to m, and at any time each
room can accommodate at most k patients (including the one being treated) .

For simplicity's sake, time is always counted/represented in minutes.

When a patient enters the hospital, he first checks the information at the reception desk, and then is transferred to a service room with least patients (ties are break by the smallest room number). Of course, if all rooms are full, this patient can not be
accepted and he has to leave at once. Assume the process of choosing room and transferring patient takes no time. And the patients always come at the beginning of a minute.

When the patient reaches his service room, the treatment could be start at once if he is the only patient in the room, otherwise he has to wait in line, since the patients in the same room will be treated one by one in the their entering order. When a patient's
treatment is done, he will leave at once and the next patient could be treated at once, if any.

Now, given the records of all patients with their name and the time when they come, please simulate the whole process of the hospital's service and print the results of the patients using the format described below.

Input

This problem has multiple test cases, which is indicated by an integer T ( T < 10 ) in the first line of the input.

The first line of each test case contains two integers m ( 1 ≤ m ≤ 10 ), k ( 1 ≤ k ≤ 10 ), the number of service rooms and the maximum number of patients that can be accommodated in each room. The next line contains an integer N (N < 100 ), the number of patients.
Then N lines follow. Each line contains the description of a patient; specifically, there is a string S (consisting of less than 10 letters and/or digits) and two integers Tb ( 1 ≤ Tb ≤ 10000 ), Ts ( 1 ≤Ts ≤ 100 ). S is the name of this patient. Tb is the
time when this patient comes to the hospital. Ts is the number of minutes needed to treat this patient (not including the waiting time). All the descriptions will be given in the ascending order of Tb. And it's guaranteed that no two patients come to the hospital
at the same time.

Note, it is possible that at a certain minute, in a full room, one patient gets his treatment done and leaves, while another patient comes to the hospital and is transferred to that room.

Output

For each test case, print the
4000
results of all patients in the order of input. Result should be one of the following (note also the format):

Patient XX was not accepted at time XX      

Patient XX had the treatment done in room XX at time XX

Print a blank line after each test case.

Sample Input
1

2 1

4

Alice 5 10

Bob 6 10

Carl 10 5

Dick 15 10

Sample Output
Patient Alice had the treatment done in room 1 at time 15

Patient Bob had the treatment done in room 2 at time 16

Patient Carl was not accepted at time 10

Patient Dick had the treatment done in room 1 at time 25

题意:病人看病,有m个房间,每间房间最多容纳看k个病人,一个房间只有一个医生,有n个病人,给出每个病人的名字,前来看病的时间还有看病所需时间,对于每一个病人,输出他所在的房间,以及看病后的时间,格式为Patient XX had the treatment done in room XX at time XX     如果该病人来的时候房间都满了,病人立即离开,输出离开时间,格式为Patient
XX was not accepted at time XX        病人选房间法则:哪个房间人少去哪个,相等时选房间号最小

题解:用队列模拟,记录等待时间

#include <stdio.h>
#include <string.h>
#include<queue>
#include <algorithm>
using namespace std;

struct person
{
int star;
int wait;
int val;
char name[10];
} zb;

queue<person>room[10];

int main()
{
int n,m,k,t;
scanf("%d",&t);
while(t--)
{
int tnum=0;
scanf("%d %d %d",&m,&k,&n);
for(int i=0; i<m; i++)
while(!room[i].empty())
room[i].pop();
while(n--)
{
getchar();
tnum=0;
scanf("%s %d %d",zb.name,&zb.star,&zb.val);
zb.wait=0;
for(int i=0; i<m; i++)
{
struct person p=room[i].front();
while(!room[i].empty()&&p.val+p.wait+p.star<=zb.star)
{
room[i].pop();;
if(room[i].size()==0) break;
p=room[i].front();
}
if(room[tnum].size()>room[i].size())
tnum=i;
}
if(room[tnum].size()>=k)
printf("Patient %s was not accepted at time %d\n",zb.name,zb.star);
else
{
if(room[tnum].size()==0)
{
printf("Patient %s had the treatment done in room %d at time %d\n",zb.name,tnum+1,zb.wait+zb.val+zb.star);
room[tnum].push(zb);
}
else
{
struct person q=room[tnum].back();
zb.wait=q.wait+q.val-(zb.star-q.star);
printf("Patient %s had the treatment done in room %d at time %d\n",zb.name,tnum+1,zb.wait+zb.val+zb.star);
room[tnum].push(zb);
}
}
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: