PAT_A1014 Waiting in Line

1014 Waiting in Line (30 分)

Author: CHEN, Yue
Unit: Zhejiang University
Time limit: 400 ms
Memory Limit: 64 MB
Code length limit: 16 KB

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customeri​​ will take Ti​​ minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1​​ is served at window1​​ while customer2​​ is served at window2​​. Customer3​​ will wait in front of window1​​ and customer4​​ will wait in front of window2​​. Customer5​​ will wait behind the yellow line.

At 08:01, customer1​​ is done and customer5​​ enters the line in front of window1​​ since that line seems shorter now. Customer2​​ will leave at 08:02, customer4​​ at 08:06, customer3​​ at 08:07, and finally customer5​​ at 08:10.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤, number of windows), M (≤, the maximum capacity of each line inside the yellow line), K (≤, number of customers), and Q (≤, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output Specification:

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry instead.

Sample Input:

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output:

08:07
08:06
08:10
17:00
Sorry

关于这道题,我一开始觉得就是个弱智题(然后分分钟打脸了,虽然是模拟,想一想还是会的,但是我的方法还是出了点问题。
我是怎么写的呢,我模拟所有人进入黄线并且占满了所有的窗口,接着遍历所有的窗口,得到所有窗口的第一个人完成的时间(同时也更新那个人的完成时间),找到最小的,把那个人出队列,将目前排在黄线外的第一个人加入到该队列中,一直到黄线外没有人,于是进行每一个窗口的出队列。
方法是先判是否为空,然后非空出队列,然后while循环更新该窗口(实际上就是更新该人的完成时间)然后出队列直到该队列为空。当我们更新完所有队列后,就能得到所有人的完成时间。
当一个人超时时,把小时赋值为100,代表sorry。
我一开始的代码有俩个bug导致只过了一个测试数据(真的是快乐模拟题,太快乐了):
  1.当一个人进队列没超时,但是出队列超时时,仍然有效不是sorry。(所以判断sorry的时候先判断该队列上一个人是否到达17:00,到达或者超过则该队列之后就都是sorry,如果上一个没到达本次更新的人不管有没有超时直接算。
  2.当人数并不能排满所有窗口*黄线时,直接更新每个窗口。
AC代码:
#include <stdio.h> 
#include <Queue> 
a using namespace std; 
struct the Node { 
	int the above mentioned id; // No, in fact, feel no need 
	int time; time // Officer 
	int window; // in a few window 
	int hh; // finish things h 
	int mm; // finish things min 
} Business [1005]; 
queue <int> que [25]; // window queue 
int hh [25]; // current time window for each 
int mm [ 25]; 
int n-, m, K, Q, p_num; // p_num was added a few people 
void update (int i) // update information, i is the number of windows 
{ 
	int que TEMP = [I] .front (); 
	if (hh [i]> = 17) // At this time when the window of the first personal beginning (start to) the time for business, if it reaches or exceeds 17:00 to 17:00 are Sorry, the assignment hh 100 to facilitate determination 
	{ 
		HH [I] = 100; 
		Business [TEMP] .hh = 100; 
	} 
		mm [I] + = Business [TEMP] .time; 
		HH [I] mm = + [I] / 60; 
	the else // finish things he or update time, and the time when he left the queue is just beginning the next person (yet start) time to conduct business, because hh [i] and mm [i] has also been updated, the time can be updated queue
	{ 
	for (int I =. 1; I <= n-m *; I ++)
		mm [I] = 60%; 
		Business [TEMP] .hh = [I] HH; 
		Business [TEMP] .mm = mm [I]; 
	} 
} 
int mintime () // return all windows finish of each window after a person's business, the minimum time at this time, return to the window number 
{ 
	int = MINP 0x3FFFFFFF, Mini; 
	for (int I =. 1; I <= n-; I ++) 
	{ 
		int timei HH = [I] * 60 + mm [I]; 
		IF (timei <MINP) 
		{ 
			MINP = timei; 
			Mini = I; 
		} 
	} 
	return Mini; 
} 
int main (void) 
{ 
	int at_which_windows = 0; // when the yellow line window row (row because the yellow line obtained in order from. 1 ~ n- 
	int Query; // interrogation frequency 
	Scanf ( "% D% D% D% D", & n-, & m, & K, & Q); 
	Fill (HH +. 1, HH + n-+. 1,. 8) ; 
	{ 
		Scanf ( "% D", Business & [I] .time); 
		Business [I] = I .id; 
		IF (at_which_windows == n) from the n // loop back window. 1 
			at_which_windows = 0; 
		at_which_windows ++; 
		Business [I] = .window at_which_windows; 
		que [at_which_windows] .push (I) ; 
		p_num ++; 
		IF (p_num> = K) // less than the number of rows of the yellow line exit 
			BREAK; 
	} 
	for (int I =. 1; I <= n-; I ++) // End window to update each individual engaged in a first time 
	{ 
		Update (I); 
	} 
	the while (p_num <K) // if someone is still beyond the yellow lines 
	{ 
		int = mintime Mint (); // can find the fastest personal business end of the first window 
		que [mint]. pop (); // end of the first personal business, then the other windows full of people (initially lined with yellow lines, all the windows are full of people, and now the end of a person's business, is certainly full of other windows At this time in line with the principle of the least people, 
// the first person outside the line just out of the queue into the queue to go, as long as people are still outside the yellow line, then there is no addition to just the queue queue queue people are not full situation closely, because someone, which is not full will make up just outside the line, if the end at the same time, a small window will be a queue number,
The next will be a queue // and large have to wait until a minimum time to find, although both the same time. Scanf ( "% D", Business & [++ p_num] .time); Business [p_num] .id = p_num; Business [p_num] .window = Mint; que [Mint] .push (p_num); Update (Mint); // end of a newly updated according to the time sequence, because other windows did not move, the first end of the time-invariant } for (int I =. 1; I <= n-; I ++) { ! IF (que [I]. empty ()) // Why should first out queue? Because prior to the last step is to update the queue, the first person actually calculated time for the queue, if the queue is not, it will repeat the calculation time of the queue will be wrong que [I] .pop (); the while ( ! que [I] .empty ()) { update (I); // after the queue is updated, because we want to update the service information finishing the id que [I] .pop (); } } for (int I = 0; I <Q; I ++) { Scanf ( "% D", & Query); IF (! Business [Query] .hh = 100) the printf ( "% 02d:% 02d \ n-", Business [Query] .hh, Business [Query] .mm); the else printf ( " } return 0; }

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jacobfun/p/11918967.html