欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT甲级1014 Waiting in Line (30 分)

程序员文章站 2024-03-17 23:41:52
...

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.
  • 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 window2Customer3 will wait in front of window1 and customer4 will wait in front of window2Customer5 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 (≤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

题目大意:

         一个银行有n个窗口并且每个窗口后面能排m个人,若每个窗口的队伍都排满了m个人则下一个人将会进入队伍最短的那列,如果有两列队伍的长度一致,则选择窗口序号最短的去排队。第一行将给出窗口数n,能排的队伍长度m、k个顾客和进行q次查询。每次查询都输出查询的客户业务结束的时间。随后一行是k个顾客各自的业务处理时间,最后一行是查询的顾客名单。

 

思路:

题目明确讲出是先把队伍排完再决定还未服务的顾客的排队问题,映射于现实生活中一开门就人满为患的银行。

数据结构,采用node1和node2结构体,分别存储每个窗户的排队与每个窗户当前客户结束业务时的时间,还有当前窗口所有人处理完成时的时间;node2则作为客户的数组,存储客户处理业务的时间以及结束业务的时间。

特别注意,窗户结构体的队列可以存储客户的名字或者直接存储客户处理业务所花费的时间,只不过如果存储了客户的名字则可能要写成customer[ windows[idx].q.front() ],来计算当前窗口处理完成的总用时。还有要明白到客户一排进队伍他的结束时间就确定了,其结束时间是窗口的总结束时间。

 

参考代码:

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
struct node1{
	queue<int> q;
	int poptime, endtime;
}; 
struct node2{
	int cost, endtime;
};
vector<node1> windows;
vector<node2> customer;
vector<bool> sorry;
int main(){
	int n, m, k, q, closetime = (17 - 8) * 60, starttime = 8 * 60;
	scanf("%d%d%d%d", &n, &m, &k, &q);
	windows.resize(n), customer.resize(k + 1), sorry.resize(k + 1);
	for(int i = 1; i <= k; ++i)	scanf("%d", &customer[i].cost);
	int num = 1;
	for(int i = 0; i < m; ++i)
		for(int j = 0; j < n; ++j)
			if(num <= k){
			windows[j].q.push(customer[num].cost);
			if(windows[j].endtime >= closetime)	sorry[num] = true;
			customer[num].endtime = windows[j].endtime += customer[num].cost;
			if(i == 0)	windows[j].poptime = windows[j].endtime;
			num++;
			}
	while(num <= k){
		int idx = 0;
		for(int i = 1; i < n; ++i)
			if(windows[i].poptime < windows[idx].poptime)	idx = i;
		if(windows[idx].endtime >= closetime)	sorry[num] = true;
		windows[idx].q.pop();
		windows[idx].q.push(customer[num].cost);
		customer[num].endtime = windows[idx].endtime += customer[num].cost;
		windows[idx].poptime += windows[idx].q.front();
		num++;
	}
	for(int i = 0; i < q; ++i){
		int id;
		scanf("%d", &id);
		if(sorry[id])	printf("Sorry\n");
		else	printf("%02d:%02d\n", (customer[id].endtime + starttime) / 60, (customer[id].endtime + starttime) % 60);
	}
	return 0;
} 

 

相关标签: PAT