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.
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;
}