Description
All the members of ICORE have been convinced that the schedule of the checkups has been professionally prepared and that there would be no lining up and waiting at the doctors' doors. However, since their boss was a political appointment their hopes for not wasting time had to be abandoned as soon as they started arriving at the hospital. The queues were forming rapidly despite the fact that the doctors were very efficient due to their usual sloppiness. The members of ICORE are all very disciplined and obey the following rules for visiting the doctors
if an ICORE member was supposed to show up at the hospital at time t, then at time t they show up at the first doctors' office on their list; if several people show up a doctor's office at time t then they form a queue in increasing order of their numbers and join the end of the queue already formed by people who arrived earlier; if at time t in front of office x there is a queue of people who arrived earlier or at time t, then the first person from the queue enters office x. This person after a time unit (the doctors do a sloppy job, remember) exits the office and at time t+1 appears at the next office from their list of offices to visit. At that time the first person from the queue enters office x; if a visit at office x at time t was for the given visitor the last visit on their list, then at time t+1 this visitor leaves the hospital.Your task is to find the time when the last visitor leaves the hospital.
The first line of input contains a natural number c giving the number of cases to handle. The following lines form the input for the c cases, each in the format described below. The first line of data for a case contains two natural numbers n and m, 1 ≤ n, m ≤ 1000, giving the number of the visitors and the number of doctors' offices for the case. Each of the following n lines contains a sequence of natural numbers. Among these lines, line i (1 ≤ i ≤ n) has the following format
For each of the c input cases print one line giving the time when the last visitor leaves the hospital.
2 5 3 1 3 3 2 1 0 7 2 3 1 1 1 1 2 2 1 1 1 2 3 3 4 3 1 1 1 5 10 3 1 6 2 3 3 2 8 2 1 4 2 4 7 9 9 6 0 2 8 7
12 6 題意:某公司要求每個員工都必須到當地的醫院體檢,並給每個員工安排了體檢的順序。為了節約等待時間,員工們被要求分時段去體檢,但排隊仍然是必不可少的。因此,公司制定了下面幾條規定:
已經知道每個醫生在每單位1的時間內可以檢查一個員工,給定所有員工的體檢時間和體檢順序,請計算一下最後一個員工離開醫院的時間。
一共有N(1 ≤ N ≤ 1000)個員工,M(1 ≤ M ≤ 1000)個醫生,所有人總的檢查次數不超過1000000次。
思路:每個項目都用一個優先隊列模擬,處理一個人後,就將這個人丟到它的下一項目對應的隊列中#include#include #include #include #include using namespace std; const int maxn = 1100; struct node { int t, id; bool operator <(const node &a) const { if (t == a.t) return id > a.id; return t > a.t; } }; int n, m; priority_queue p[maxn]; queue q[maxn]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); int x, num, a; node now; for (int i = 0; i < n; i++) { now.id = i; scanf("%d%d", &x, &num); while (num--) { scanf("%d", &a); a--; q[i].push(a); } now.t = x; p[q[i].front()].push(now); } int ans = 0, flag = 1; while (flag) { flag = 0; for (int i = 0; i < m; i++) { if (!p[i].empty()) { flag = 1; now = p[i].top(); if (now.t > ans) continue; p[i].pop(); q[now.id].pop(); if (!q[now.id].empty()) { now.t = ans + 1; p[q[now.id].front()].push(now); } } } ans++; } printf("%d\n", ans-1); } return 0; }