Ultraviolet A 10588-queuing at the doctors (priority queue)

Source: Internet
Author: User
Ultraviolet A 10588-queuing at the doctors

Question Link

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 10000000

Idea: directly use the queue and the priority queue for simulation. Open M priority queues, indicating that each doctor will give priority by time and then by label. At the beginning, I threw the sequence of doctors corresponding to each person into the priority queue. The result timed out and changed the location.

Code:

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;const int N = 1005;int T, n, m;struct Person {    int t, id;    bool operator < (const Person& c) const {if (t != c.t) return t > c.t;return id > c.id;    }} p;priority_queue<Person> Q[N];queue<int> q[N];void init() {    scanf("%d%d", &n, &m);    int k, num;    for (int i = 0; i < n; i++) {p.id = i;scanf("%d%d", &p.t, &k);for (int j = 0; j < k; j++) {    scanf("%d", &num);    num--;    q[p.id].push(num);}Q[q[p.id].front()].push(p);    }}int solve() {    int flag = 1, ans = 0;    while (flag) {flag = 0;for (int i = 0; i < m; i++) {    if (!Q[i].empty()) {flag = 1;Person now = Q[i].top();if (ans < now.t) continue;Q[i].pop();q[now.id].pop();if (!q[now.id].empty()) {    now.t = ans + 1;    Q[q[now.id].front()].push(now);}    }}ans++;    }    return ans - 1;}int main() {    scanf("%d", &T);    while (T--) {init();printf("%d\n", solve());    }    return 0;}


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.