PAT-1014 Waiting in Line (30 分)

版权声明:未经过本人同意不得转发 https://blog.csdn.net/weixin_42956785/article/details/84845891

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, customer1is served at window1while customer2is served at window . Custome3will wait in front of window1 and customer4 will wait in front of window​2 . Customer5 will wait behind the yellow line.At 08:01, customer1 is done and customer​5 enters the line in front of window1 since that line seems shorternow.Customer​2will 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

解题思路

意思: 题目的大概意思就是,银行有N个营业的窗口,每个窗口最多只能够排M个人,多的人只能在黄线外面等候,当黄线里面有空位让你排队的时候,黄线外面的人就可以排队进去。让你求这个人完成他的业务的时间是几点。每个人都对应一个完成他们手上事情的时间,且他们办理业务的顺序,由题目给出来的时间顺序。必须按照这个顺序进黄线
考点: 这题的考点就在于队列,结构体,以及对整个题目的思路清晰。题目虽然理解上不囊,但是做出来还是要死一些脑细胞的。同时,若某个客户办理业务的时间在17点之前,但完成业务的时间在17点后,则不应该输出sorry。题目的具体方法就是,每个窗口给予他一个对列,这个对列放结构体内。且每个结构体有对列首完成这项任务后的时间是几点,每次找出最先完成的对列,然后让黄线外的人排到这个对列里面。

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct bank {
	int time;
	queue<int>lines;
};
typedef struct bank Bank;
int T[1005];
int t[1005];
int main()
{
	int N, M, K, Q;
	scanf("%d%d%d%d", &N, &M, &K, &Q);
	vector<Bank>time(N + 3);
	vector<Bank>people(N + 3);
	int count = 1;
	for (int i = 1; i <= M; i++)
	{
		for (int j = 1; j <= N; j++)
		{
			if (count > K)break;
			int a;
			scanf("%d", &a);
			time[j].lines.push(a);
			t[count] = a;
			if (i == 1)
			{
				time[j].time = a;
			}
			people[j].lines.push(count);
			count++;
		}
	}
	for (count; count <= K; count++)
	{
		int min = 800;
		int index = 0;
		for (int i = 1; i <= N; i++)
		{
			if (time[i].time < min)
			{
				min = time[i].time;
				index = i;
			}
		}
		time[index].lines.pop();
		T[people[index].lines.front()] = time[index].time;
		time[index].time += time[index].lines.front();
		people[index].lines.pop();
		int a;
		scanf("%d", &a);
		t[count] = a;
		time[index].lines.push(a);
		people[index].lines.push(count);
	}
	for (int i = 1; i <= N; i++)
	{
		while (!time[i].lines.empty())
		{
			T[people[i].lines.front()] = time[i].time;
			people[i].lines.pop();
			time[i].lines.pop();
			if (time[i].lines.empty())break;
			time[i].time += time[i].lines.front();
		}
	}
	for (int i = 0; i < Q; i++)
	{
		int a;
		scanf("%d", &a);
		if (540 < T[a]&&T[a]-t[a]>=540)
		{
			//if (T[a] - t[a] < 540)printf("17:00\n");
			//else
			printf("Sorry\n");
		}
		else
		{
			int k = T[a] / 60;
			int t = T[a] % 60;
			printf("%02d:%02d\n", k + 8, t);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42956785/article/details/84845891