1026. Table Tennis (30)

Source: Internet
Author: User

The topics are as follows:

A Table Tennis Club have N tables available to the public. The tables is numbered from 1 to N. For any pair of players, if there is some tables open when they arrive, they'll be assigned to the available table with The smallest number. If all the tables is occupied, they'll has the to wait in a queue. It is assumed this every pair of players can play for at the most 2 hours.

your job is to count for everyone in queue their waiting time, and for each table the number of P Layers It has served for the day.

One thing that makes this procedure a bit complicated are the the club reserves some tables for their VIP members. When a VIP table was open, the first VIP pair in the queue would have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if-it is the turn of a VIP pair, yet no VIP table was available, they can be assigned as any Ordina Ry players.

Input Specification:

each input file contains one test case. For each case, the first line contains a integer N (<=10000)-the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag:hh:mm:ss-the arriving time, p-the playing time in minutes of A pair of players, and Tag-which is 1 if they hold a VIP card, or 0 if not. It is guaranteed, the arriving time is between 08:00:00, and 21:00:00 while the club is open. It is assumed, that no, customers arrives at the same time. Following the players ' info, there is 2 positive integers:k (<=100)-The number of tables, and M (< K)-The Numb Er of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the Forma T shown by the sample. Then print with a line the number of players served by each table. Notice that the output must is listed in chronological order of the serving time. The waiting time must is rounded up to an integer minute (s). If one cannot get a table before the closing time, their information must not be printed.

Sample Input:
920:52:00 10 008:00:00 20 008:02:00 30 020:51:00 10 008:10:00 5 008:12:00 10 120:50:00 10 008:01:30 15 120:53:00 10 13 12
Sample Output:
08:00:00 08:00:00 008:01:30 08:01:30 008:02:00 08:02:00 008:12:00 08:16:30 508:10:00 08:20:00 1020:50:00 20:50:00 020:51:0 0 20:51:00 020:52:00 20:52:00 03 3 2



The topic requires a simulation of the events of the table Tennis Hall.

According to the user arrival time queue, there are empty tables on a first-come-first service principle, if the queue has VIP users and VIP table is free, the VIP can be "self into a team", in accordance with the order of arrival directly assigned to the VIP table, if there is no VIP table free, then VIP and ordinary users are treated equally. If the team does not have VIP and the smallest number is the VIP table, ordinary users can also use the VIP table.

This kind of topic needs to deal with the table's idle and the user's arrival, the service time relations, needs to have a good idea, otherwise very easy to confuse.

A good idea is to set the idle time for each table, first of all initialized to 8:00, when dealing with a person, first from the table in front of the list of tables, and then according to their arrival time and the table's free time to calculate the table's new idle time, The user's wait time and service time (which may not reach the expected service time when closing).

This problem in the VIP, if there are VIP users, they can "queue", to deal with these users, will make the problem complicated, not simply take out the first non-service users and the first table, but to consider the VIP user and VIP table situation, here are two excellent solutions:

① is similar to the idea of merging and sorting, maintaining two queues for both ordinary users and VIP users.

② only use a queue, first consider the VIP situation, no VIP is processed in accordance with the normal situation, the algorithm from Sunbaigui.

I have a complete reference to the Sunbaigui solution, his solution is very ingenious, the following list of key points:

① to the user after the first-to-post processing, the initialization of the service start time is the INF, so that when the processing of a person, his service time is no longer an INF, thus determining whether the processing is complete, do not have to set another flag.

Instead of sorting the table, ② found all the earliest free time in the table, sifting through the table list from front to back, so that it was very ingenious in the order of the numbers.

③ based on the earliest idle time found in the ②, find all the tables and users that fit, storing a new container, followed by the two new containers.

④ Sub-situation discussion, single table, multi-table, multi-table, in these three cases to determine whether there is a VIP service.

⑤ if no VIP is served in the ④, the first user and the first table in the new container are removed for normal processing.

The following code comes from Sunbaigui, and I have added some comments on the basis of his code to make it easier to understand.

#include <iostream> #include <vector> #include <set> #include <map> #include <queue># include<algorithm> #include <string> #include <string.h> #include <stdio.h>using namespace std ; typedef struct NODE{INT arrive, process, tag;int serve, wait;//serve and wait represent service start time and wait time respectively, serve is initialized to INF, thus distinguishing between service} node;typedef struct table{int tag;int freetime, num;//each table records its own idle time and number of services, all initialized to 8:00, so that you can iterate over the}table;bool cmparrive ( Node A, node B)//used to sort the user's arrival time in ascending order {return a.arrive < b.arrive;} BOOL Cmpserve (Node A, node B)//For the user in chronological order, prioritized according to service time, if the service starts at the same time (multiple empty tables), according to the arrival time ascending {if (A.serve = = B.serve) return a.arrive & Lt B.arrive;else return A.serve < B.serve;} #define INF 0x6fffffffvector<node> user;vector<table> table;void UpdateInfo (int userID, int tableID) {user[ Userid].serve = Max (user[userid].arrive, table[tableid].freetime); user[userid].wait = user[userid].serve-user[ Userid].arrive;table[tableid].num++;table[tableid].freetime = User[userid].serve+min (useR[userid].process, 7200); The maximum service time is 2 hours}int main () {//inputint n;scanf ("%d", &n), User.resize (n), for (int i = 0; i < n; ++i) {int h, M, s;scanf ("% d:%d:%d%d%d ", &h,&m,&s,&user[i].process,&user[i].tag); user[i].arrive = H*3600+m*60+s;user[i]. Process *= 60;user[i].serve = INF; user[i].wait = INF; Initialized to Inf,inf represents an unhandled}int K, m;scanf ("%d%d", &k,&m); Table.resize (k); for (int i = 0; i < K; ++i) Table[i]. FreeTime = 8*3600, Table[i].tag = 0, table[i].num = 0; All tables are available starting from 8:00 for (int i = 0; i < m; ++i) {int c;scanf ("%d", &c); c--;table[c].tag = 1;} Processsort (User.begin (), User.end (), cmparrive); In ascending order according to arrival time, the queuing rule//visited.assign (n, false); for (int i = 0; i < n; ++i) {if (User[i].serve! = INF) continue;//server is initialized to INF, not the INF has been serviced. int minfreetime = inf;for (int j = 0; j < K; ++j) minfreetime = min (minfreetime, table[j].freetime); Find the earliest idle table int timepoint = max (user[i].arrive, minfreetime); Determine the current earliest service point if (timepoint >= 21*3600) Break according to the team header user; JudgeWhether it exceeds business hours vector<int> userlist;vector<int> tablelist;for (int j = i; j < n; ++j)//According to the earliest service time point, all the people who may be served are located for Have VIP priority to the VIP table. if (User[j].serve = = INF && user[j].arrive <= timepoint) Userlist.push_back (j); for (int j = 0; j < K; ++j)// Find all the tables that are idle before the point in time, because the user may arrive late, so there will be more than one table idle if (table[j].freetime <= timepoint) Tablelist.push_back (j); bool flag = false; Decide whether to handle a service//first special handling VIP, if no VIP is processed to deal with an ordinary user, only one time to process an if (userlist.size () = = 1 && tablelist.size () > 1)// A single user multiple tables, the user for the VIP to find the smallest VIP table {if (User[userlist[0]].tag = = 1) {for (int j = 0; J < tablelist.size (); ++j) {if (table[ Tablelist[j]].tag = = 1) {flag = true; UpdateInfo (Userlist[0], tablelist[j]); break;}}} else if (tablelist.size () = = 1 && userlist.size () > 1)//multi-table case, if the VIP table for more than the first to reach the VIP for its service {if (table[tablelist[0 ]].tag = = 1) {for (int j = 0; J < userlist.size (); ++j) {if (User[userlist[j]].tag = = 1) {flag = true; UpdateInfo (Userlist[j], tablelist[0]); break;}}} else if (tablelist.size () > 1 && uSerlist.size () > 1)//multi-table situation, first find the table VIP table, there is the first to find the user vip{for (int t = 0; t < tablelist.size (); ++t) {if (Table[tablelis T[t]].tag = = 1) {for (int u = 0; u < userlist.size (); ++u) {if (User[userlist[u]].tag = = 1) {flag = true; UpdateInfo (Userlist[u], tablelist[t]); break;}}}} if (!flag) UpdateInfo (userlist[0], tablelist[0]); If no VIP is processed, it is handled as normal. -----I.} Outputsort (User.begin (), User.end (), cmpserve); for (int i = 0; i < n; ++i) {if (User[i].serve >= 21*3600) break;int h 1, M1, S1, H2, m2, s2;int t = user[i].arrive;h1 = t/3600; T%= 3600;m1 = T/60; T%= 60;s1 = t;t = User[i].serve;h2 = t/3600; T%= 3600;m2 = T/60; T%= 60;s2 = t;//Note The rounding of the wait time for more than 30 seconds counts as one minute printf ("%02d:%02d:%02d%02d:%02d:%02d%d\n", H1, M1, S1, H2, M2, S2, (User[i].wai T+30)/60);} for (int i = 0; i < K; ++i) {if (i! = k-1) printf ("%d", table[i].num), Else printf ("%d\n", Table[i].num);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1026. Table Tennis (30)

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.