1. 程式人生 > >PAT 1014 Waiting in Line (30 分)

PAT 1014 Waiting in Line (30 分)

1014 Waiting in Line (30 分)

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 (NM+1)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. ③C

ustomeriCustomer_i will take TiT_i 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, c
ustomer1customer_1
is served at window1window_ 1while customer2customer​_2is served at window2window_2. Customer3Customer_3will wait in front of window1window_1and customer4customer_4will wait in front of window2window_2. Cust
omer5Customer_5
will wait behind the yellow line. At 08:01, customer1customer_1 is done and customer5customer_5 enters the line in front of window1window_1 since that line seems shorter now. Customer2Customer_2 will leave at 08:02, customer4customer_4 at 08:06, customer3customer_3 ​​ at 08:07, and finally customer5customer_5 ​​ at 08:10. Input Specification: Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (20≤20, number of windows), M (10≤10, the maximum capacity of each line inside the yellow line), K (1000≤1000, number of customers), and Q (1000≤1000, 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

解析

這題對我來說挺難的,我花了大概4個小時才做完。 第一次提交只有一個測試點過了.看到這個帖子:浙大pat1014 Waiting in Line 求助。第二次AC了。 如果你沒過:可以試試下面的測試資料:

input: 
2 2 9 9
1 2 6 4 3 1 9 100 534
1 2 3 4 5 6 7 8 9
output:
08:01 08:02 08:07 08:06 08:10 08:07 08:16 09:50 17:10

我再看看了題意: 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. 這裡是cannot be served,是指請求服務要在17:00前,也就是允許17:00後還進行處理。但是不能在17:00後請求處理。醉了≡(▔﹏▔)≡ 具體的思路:N個視窗,M長的佇列。我用vector<queue>來模擬。首先:把佇列填滿(對應30~39行程式碼)。再開始開始事件迴圈。也就是先選出N個佇列中首元素中:處理時間最少的人(對應45~53行程式碼)設時間是min。因為他肯定是最先處理完畢的。當然:佇列中其他的首元素也要減去這個時間min。因為它們也在處理,只是慢一些。(對應54~57行程式碼)。這個人處理完後,把他pop出佇列,這個時候要考慮把其他客戶壓進隊列了。這裡還要處理客戶都已經被處理了。不用把客戶壓入隊列了(60~75行程式碼).最後就是收尾工作了,記錄時間(76~80) 這裡我記錄了2個時間:客戶剛開始處理的時間,和客戶處理完後的時間。具體可以看Code Code:

#include<vector>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
#include<string>
#include<utility>
using namespace std;
const unsigned DEADLINE = 540;
pair<int,int> _time(int t) {
	int hour = 8 + t / 60;
	int min = t % 60;
	return make_pair(hour, min);
}
int main()
{
	int N, M,K,Q;
	cin >> N >> M >> K >> Q;
	vector<pair<int, int>> traction(K+1, make_pair(0, 0));
	vector<int> process(K+1,0),Query(Q, 0);
	for (int i = 1; i <= K; i++) {
		cin >> traction[i].first;
		traction[i].second = DEADLINE+1;
	}
	for (int i = 0; i < Q; i++)
		cin >> Query[i];
	vector<queue<int>> QUEUE(N);     //N windows ,M wating
	int current = 1;
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < N; j++) {
			if (current != K+1)
				QUEUE[j].push(current++);
			else
				break;
		}
		if (current == K+1)
			break;
	}
	int time = 0,index=0;
	bool flag = true;
	while (flag){
		pair<int, int> min = make_pair(65536, 0);
		int j = 0;
		for (int i = 0; i < N; i++) {    //loop Windows
			if (!QUEUE[i].empty()) {
				if (min.first > traction[QUEUE[i].front()].first) {     //QUEUE[i].front is cumstoms th;
					min.first = traction[QUEUE[i].front()].first;      //traction time
					min.second = i;                                   //which window?
					j = QUEUE[i].front();
				}
			}
		}
		for (int i = 0; i < N; i++) {//every one in windows`s first should sub min`s time.because they are processding at same time
			if (!QUEUE[i].empty()) {
				traction[QUEUE[i].front()].first-= min.first;
			}
		}
		QUEUE[min.second].pop();
		if (current != K+1) {
			pair<int, int> less_windows = make_pair(M + 1, -1);
			for (int i = 0; i < N; i++) {
				if (less_windows.first > QUEUE[i].size()) {
					less_windows.first = QUEUE[i].size();
					less_windows.second = i;
				}
			}
			process[QUEUE[less_windows.second].front()]=time+min.first;
			QUEUE[less_windows.second].push(current++);
		}
		else {        //沒人排隊了
			if(QUEUE[min.second].size()>=1)
				process[QUEUE[min.second].front()] = time + min.first;
		}
		time += min.first;
		traction[j].second = time;
		flag = !QUEUE[0].empty();
		for (int i = 1; i < N; i++)
			flag = flag | !QUEUE[i].empty();
	}

	for (int i = 0; i < Q; i++) {
		if (process[Query[i]] >= DEADLINE)
			printf("Sorry\n");
		else {
			auto temp = _time(traction[Query[i]].second);
			printf("%02d:%02d\n",temp.first,temp.second);
		}
	}
	return 0;
}