PAT A1014 Waiting in Line (30point(s))

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.
  • Customer​i​​ will take 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, customer​1​​ is served at window​1​​ while customer​2​​ is served at window​2​​. Customer​3​​ will wait in front of window1​​ and customer​4​​ will wait in front of window​2​​. Customer​5​​ will wait behind the yellow line.

At 08:01, customer​1​​ is done and customer​5​​ enters the line in front of window​1​​ since that line seems shorter now. Customer​2​​ will leave at 08:02, customer​4​​ at 08:06, customer​3​​ at 08:07, and finally 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, number of windows), M (≤10, the maximum capacity of each line inside the yellow line), K (≤1000, number of customers), and Q (≤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
  • 思路 1:枚举分钟

由于单位是分钟,且数量级不大(8点到18点 = 10 * 60 = 600 分钟),考虑枚举每一分钟的情况,即每过一分钟处理一次所有窗口(<=20),可以看出时间复杂度为: 600 * 20 = 12000 完全可以接受

> step 1:初始化

先依次进入黄线,循环站在各窗口前,直到达到容量(N * M), 或者没人了,并初始化每个窗口i当前服务的结束时间 end_time[i](即第一排顾客的处理时间 times[i]

> step 2 :枚举分钟,每过一分钟,从小到大依次遍历每个窗口,检查是否有窗口服务结束:

假设第j个窗口服务结束(即:end_time[j] == 当前时间 clock

将窗口j队列的队首front出队(这个人搞完了),并记录其信息:包括 出队时间 (= clock),入队时间(= clock - times[front]),其中出队时间可以不记录,直接用入队时间+处理时间即可

如果他后面还有人(队不空),为后面那个人服务(更新end_time[j]

如果还有人在黄线外(idex <= num_p),将其加入队列

【注】 因为枚举的是分钟,每分钟一个队列只能出1个人,初始化将黄线内装满了,每出去一个人,就自动补上一个(若外面还有人);从大到小枚举窗口满足编号最小优先

> step 3

对每个查询id,检查其入队,出队时间,若入队时间晚于17:00,输出Sorry
否则,将时间转化为HH:MM输出其出队时间

  • code :
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010, time_17 = 9 * 60, time_18 = 10 * 60;
int times[maxn], in_q[maxn], end_time[25];
queue<int> win[25];

int idex = 1;
void Init(int num_P, int num_W, int capacity){
	for(int i = 1; i <= capacity; ++i){
		for(int j = 1; j <= num_W; ++j){
			win[j].push(idex);
			if(idex > num_P) return;
			else idex++;
		}
	}
} 
int main(){
	int num_w, cap, num_p, num_q;
	scanf("%d %d %d %d", &num_w, &cap, &num_p, &num_q);
	for(int i = 1; i <= num_p; ++i){
		scanf("%d", &times[i]);
		if(i <= num_w) end_time[i] = times[i];	//step 1: 初始化每个窗口的第一轮结束时间 
	} 	
	Init(num_p, num_w, cap);	//step 1: 初始化黄线内 
	for(int clock = 0; clock < time_18; ++clock){
		for(int j = 1; j <= num_w; ++j){	//step 2: 检查是否有窗口服务结束
			if(!win[j].empty() && end_time[j] == clock){	//step 2;窗口j 服务结束 
				int tmp = win[j].front(); 	//记录这个人的各项信息:出队时间(结束服务时间),以及入队时间 
				in_q[tmp] = clock - times[tmp];
				win[j].pop();
				if(!win[j].empty()) end_time[j] = clock + times[win[j].front()];
				if(idex <= num_p) win[j].push(idex++);	
			}
		}
	}	
	for(int i = 1; i <= num_q; ++i){
		int tmp_q;
		scanf("%d", &tmp_q);
		int in = in_q[tmp_q], out = in_q[tmp_q] + times[tmp_q];
		if(in >= time_17) printf("Sorry\n");	
		else printf("%02d:%02d\n", out / 60 + 8,  out % 60);
	}
	return 0;
}
发布了271 篇原创文章 · 获赞 5 · 访问量 6517

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/104188260