Ultraviolet A-10588 queuing at the doctors (Queue)

Source: Internet
Author: User

Description

Problem H: queuing at the doctors due to the increasing number of weird viruses spreading around, all the members of the International Confederation of revolver enthusiasts (icore) are required by their boss to do quarterly Physical checkups at General Hospital. all checkups are arranged by the boss and scheduled on the same day. each member of icore gets instructions where they are given
  • Their number from the set {1...N}
  • The time of the day when they are supposed to show up at General Hospital
  • A list of doctors 'offices that they are to visit in the listed order.
Doctors 'offices in general hospital are numbered with numbers from the set {1... M}.

All the members of icore have been convinced that the schedule of the checkups has been already sionally prepared and that there wocould 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 hosping. 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 timeT, Then at timeTThey show up at the first doctors 'Office on their list;
  • If several people show up a doctor's office at timeTThen 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 timeTIn front of officeXThere is a queue of people who arrived earlier or at timeT, Then the first person from the queue enters OfficeX. This person after a time unit (the doctors do a sloppy job, remember) exits the Office and at timeT + 1Appears at the next office from their list of offices to visit. At that time the first person from the queue enters OfficeX;
  • If a visit at OfficeXAt timeTWas for the given visitor the last visit on their list, then at timeT + 1This 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 numberCGiving the number of cases to handle. The following lines form the input forCCases, each in the format described below. The first line of data for a case contains two natural numbersNAndM, 1 ≤N,M≤ 1000, giving the number of the visitors and the number of doctors 'offices for the case. Each of the followingNLines contains a sequence of natural numbers. Among these lines, lineI(1 ≤IN) Has the following format

T K g1 G2... GK Meaning that ITh visitor arrives at time TAnd has to visit KOffices in the order given G1 G2... GK where each GJ is a number of doctor's office, 1 ≤ GJ ≤ M. We have that 0 ≤ T≤ 1000000 and there is no more than 1000000 visits scheduled for a day at the hospital.

For each ofCInput cases print one line giving the time when the last visitor leaves the hospital.





Sample Input
 25 31 3 3 2 10 7 2 3 1 1 1 1 22 1 11 2 3 34 3 1 1 15 103 1 62 3 3 2 82 1 42 4 7 9 9 60 2 8 7
Output for sample input
126 question: A company requires every employee to go to the local hospital for examination and arrange the sequence of examination for each employee. In order to save the waiting time, employees are required to perform health checks in different time periods, but queuing is still essential. Therefore, the company has formulated the following rules:
 
 
  • The employee ID ranges from 1 to n.
  • The employee must arrive at the hospital on time at the specified time to start the examination.
  • The employee has his/her own inspection order, and must perform the examination in sequence until the examination is complete.
  • When multiple employees go to the same doctor for health check at the same time, the smaller the number is given priority. Others wait in line according to the order of arrival and the number size.

We already know that each doctor can check an employee within the time of every unit 1. for the given time and sequence of inspection for all employees, calculate the time when the last employee leaves the hospital.

A total of N (1 ≤ n ≤ 1000) Employees, M (1 ≤ m ≤ 1000) doctors, the total number of examinations for all people does not exceed 1000000.

Idea: Each project uses a priority queue simulation. After processing a person, the person will be thrown into the queue corresponding to the next project.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>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<node> p[maxn];queue<int> 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;}


Ultraviolet A-10588 queuing at the doctors (Queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.