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 u s t o m e r i Customer_i will take T i T_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 u s t o m e r 1 customer_1 is served at w i n d o w 1 window_ 1 while c u s t o m e r 2 customer​_2 is served at w i n d o w 2 window_2 . C u s t o m e r 3 Customer_3 will wait in front of w i n d o w 1 window_1 and c u s t o m e r 4 customer_4 will wait in front of w i n d o w 2 window_2 . C u s t o m e r 5 Customer_5 will wait behind the yellow line.
At 08:01, c u s t o m e r 1 customer_1 is done and c u s t o m e r 5 customer_5 enters the line in front of w i n d o w 1 window_1 since that line seems shorter now. C u s t o m e r 2 Customer_2 will leave at 08:02, c u s t o m e r 4 customer_4 at 08:06, c u s t o m e r 3 customer_3
​​ at 08:07, and finally c u s t o m e r 5 customer_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;
}

猜你喜欢

转载自blog.csdn.net/weixin_41256413/article/details/82870288