天天看点

PAT甲级1014 Waiting in Line//复杂模拟

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 customers 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 window​1​​ 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, customer3​​ 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个人,如果所有窗口都排满M个人则其余都站黄线后面。每当有窗口前的人没排满,黄线外的人就找人最少的窗口去排队。业务处理从8:00开始,17:00结束。在17:00之前开始业务,但是一直到下班都还没结束的话也同样会继续处理,直到结束。但是在17:00之后不能再进行业务处理了。最后给出Q个人,要求这Q个人的业务处理结束时间。

思路

这里先将时间量化为数字,8:00量化为640,17:00量化为1020,这样设置能更方便的判断时间范围。然后每个人量化后的业务开始时间都必须在区间[640, 1020)内。

接着模拟这个业务处理的流程即可。设置一个window结构体数组,每个结构体都维持一个队列表示每个窗口排队的人,再设置一个out队列表示黄线外排队的人。循环寻找所有窗口前的第一个人中需要处理的时间最短的那个人,假设他的处理时间是t,把这个人弹出队列,接着让所有窗口前第一个人的处理时间都减去t,这样就模拟了一个人业务处理完的过程,然后把黄线外的那个人加入到刚刚的窗口队列中,继续循环上述过程。

设置一个散列表,在一个人业务处理结束即将弹出队列的时候给他映射一个量化的时间表示他业务结束的时间。最后每个人都会有个业务结束时间。用业务结束时间减去处理时间能得出业务开始时间,最后只输出业务开始时间在区间[640, 1020)内人。

#include <iostream>
#include <cstdlib>
#include <queue>
#include <map>
#include <vector>
using namespace std;
struct customer
{
	int id;
	int need_time;
	customer(int a, int b) : id(a), need_time(b) {}
};
struct window
{
	queue<customer> wait;
} win[20];
int main()
{
	int n, m, k, q;
	cin >> n >> m >> k >> q;
	map<int, int> mp;
	queue<customer> out; //存放黄线外排队的人
	vector<int> needTime; //存放业务处理时间
	for (int i = 0; i < k; i++)
	{
		int time, size = m, loc = -1; //size表示每个窗口前最少的人数量,loc表示窗口位置
		cin >> time;
		needTime.push_back(time);
		for (int j = 0; j < n; j++) //这个循环用来找人最少的窗口
		{
			if (win[j].wait.size() <= m && win[j].wait.size() < size)
			{
				size = win[j].wait.size();
				loc = j;
			}
		}
		customer s = customer(i + 1, time);
		if (loc != -1)
			win[loc].wait.push(s);
		else //窗口全满就放入out队列
			out.push(s);
	}
	vector<int> ans(q);
	for (int i = 0; i < q; i++)
		cin >> ans[i];
	int start = 480, end = 1020, index = 480; //量化时间
	while (k > 0)
	{
		int loc = 0, min_time = 9999, who = 0;
		for (int i = 0; i < n; i++) //这个循环找每个窗口前需要时间最短的客户
		{
			if (win[i].wait.empty())
				continue;
			else if (win[i].wait.front().need_time < min_time)
			{
				min_time = win[i].wait.front().need_time;
				loc = i;
				who = win[i].wait.front().id;
			}
		}
		for (int i = 0; i < n; i++) //让每个窗口前第一个客户都减去这个时间,因为业务是同时处理的
			if (win[i].wait.size() != 0)
				win[i].wait.front().need_time -= min_time;
		win[loc].wait.pop(); //弹出
		k--; //客户数减1
		index += min_time; //index加上这个时间,表示当前客户的结束时间
		mp[who] = index;
		if (out.size() > 0) //out队列补上一个客户
		{
			win[loc].wait.push(out.front());
			out.pop();
		}
	}
	for (int i = 0; i < ans.size(); i++)
	{
		int time = mp[ans[i]];
		int start_time = time - needTime[ans[i] - 1]; //结束时间-处理时间=开始时间
		int h = time / 60;
		int m = time % 60;
		if (start_time >= 1020) //开始时间在17:00及之后的不输出
			cout << "Sorry\n";
		else
			printf("%02d:%02d\n", h, m);
	}
	system("pause");
	return 0;
}