Hospital(队列+模拟)

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 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;
}


队列应用(用队列模拟超市交款处的顾客流) 使用一个队列模拟一队通过丹尼斯超市交款处的顾客流。为了创建这个模拟,我们必须模拟排队时间和顾客通过流。我们可以通过一个循环模拟时间,每通过一个顾客代表一定的时间间隔——例如,一分钟。我们可以使用一个队列模拟顾客流,队列中的一个数据项代表一位顾客。 为了完成这个模拟,我们需要知道顾客加入交款处队列的频率、交款结算服务情况和离开的频率。假设交款队列有以下属性。 • 每分钟有一个顾客完成交款并离开(假设此时至少有一个顾客等待服务)。 • 每分钟有零个到两个顾客加入,没有顾客到达的概率是50% , 一个顾客到达的概率是 25 % ,两个顾客到达的概率是 25 %。(如何模拟?) 我们可以使用下面的算法模拟一个时间段 n 分钟内的顾客流。 初始化队列为空。 for (minute = 0 ; minute < n ; + + minute) { 如果队列不空,对头顾客交款并离开(即出对); 产生一个0-3范围内的随机数k; 如果k=1,一个顾客加入交款队列(入对); 如果k=2,两个顾客加入交款队列(入对); 如果k=0或3,不增加任何顾客到交款队列; } 调用 rand ( )函数是产生伪随机数的一种简单的方法,rand函数在中。 我们的模拟程序应该在每一个模拟分钟期间内更新下列信息,即每一次通过循环。 • 完成交款服务的总顾客数 • 这些顾客花费在排队等待的时间总和 • 顾客花费在排队等待的最长时间 为了计算顾客等待的时间长度,我们需要存储“minute”,作为这个客户队列数据项的一部分,表示顾客加入的时间。 如果你使用程序模拟一列顾客流,试着完成下面的表格。请注意,平均等待时间是等待时间总和除以总的服务顾客数。
以下是使用C++队列实现的医院挂号系统的代码: ``` #include <iostream> #include <string> #include <queue> using namespace std; class Patient { private: string name; int age; string gender; string disease; public: Patient(string n, int a, string g, string d) { name = n; age = a; gender = g; disease = d; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Gender: " << gender << endl; cout << "Disease: " << disease << endl; } }; class Hospital { private: queue<Patient> patients; public: void addPatient(Patient p) { patients.push(p); cout << "Patient added successfully!" << endl; } void displayPatients() { if (patients.empty()) { cout << "No patients in queue." << endl; return; } queue<Patient> temp(patients); while (!temp.empty()) { temp.front().display(); cout << endl; temp.pop(); } } void servePatient() { if (patients.empty()) { cout << "No patients in queue." << endl; return; } patients.front().display(); cout << " has been served." << endl; patients.pop(); } }; int main() { Hospital hospital; string name, gender, disease; int age; int choice; do { cout << "1. Add Patient" << endl; cout << "2. Display Patients" << endl; cout << "3. Serve Patient" << endl; cout << "4. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: cout << "Enter patient name: "; cin >> name; cout << "Enter patient age: "; cin >> age; cout << "Enter patient gender: "; cin >> gender; cout << "Enter patient disease: "; cin >> disease; Patient p(name, age, gender, disease); hospital.addPatient(p); break; case 2: hospital.displayPatients(); break; case 3: hospital.servePatient(); break; case 4: cout << "Exiting program..." << endl; break; default: cout << "Invalid choice! Try again." << endl; break; } } while (choice != 4); return 0; } ``` 这个程序使用了STL库中的queue来实现医院挂号系统。Patient类表示患者,包含姓名、年龄、性别和疾病等信息,并有一个display方法用于显示患者信息。Hospital类表示医院,包含一个患者队列,有三个方法:addPatient用于添加患者到队列中,displayPatients用于显示所有患者的信息,servePatient用于表示医院已经为队列中的第一个患者提供了服务。 在主函数中,我们首先创建了一个Hospital对象,并通过一个do-while循环来提供用户菜单选择。用户可以选择添加患者、显示所有患者信息、为队列中的第一个患者提供服务或退出程序。当用户选择添加患者时,程序会提示用户输入患者姓名、年龄、性别和疾病等信息,并创建一个Patient对象,然后将该对象添加到Hospital对象的患者队列中。当用户选择显示所有患者信息时,程序会遍历Hospital对象的患者队列,并调用每个患者对象的display方法来显示其信息。当用户选择为队列中的第一个患者提供服务时,程序会显示第一个患者的信息,并将其从患者队列中移除。 这个程序可以作为医院挂号系统的一个简单实现,使用队列来存储患者信息,保证了按照先来后到的顺序进行服务。同时,队列的FIFO(先进先出)特性也符合了医院挂号系统的实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值