PAT_A1014 Waiting in Line

1014 Waiting in Line (30 分)

作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

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 (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 window2​​. Customer3​​ will wait in front of window1​​ and customer4​​ will wait in front of window2​​. Customer5​​ 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 (≤, number of windows), M (≤, the maximum capacity of each line inside the yellow line), K (≤, number of customers), and Q (≤, 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

关于这道题,我一开始觉得就是个弱智题(然后分分钟打脸了,虽然是模拟,想一想还是会的,但是我的方法还是出了点问题。
我是怎么写的呢,我模拟所有人进入黄线并且占满了所有的窗口,接着遍历所有的窗口,得到所有窗口的第一个人完成的时间(同时也更新那个人的完成时间),找到最小的,把那个人出队列,将目前排在黄线外的第一个人加入到该队列中,一直到黄线外没有人,于是进行每一个窗口的出队列。
方法是先判是否为空,然后非空出队列,然后while循环更新该窗口(实际上就是更新该人的完成时间)然后出队列直到该队列为空。当我们更新完所有队列后,就能得到所有人的完成时间。
当一个人超时时,把小时赋值为100,代表sorry。
我一开始的代码有俩个bug导致只过了一个测试数据(真的是快乐模拟题,太快乐了):
  1.当一个人进队列没超时,但是出队列超时时,仍然有效不是sorry。(所以判断sorry的时候先判断该队列上一个人是否到达17:00,到达或者超过则该队列之后就都是sorry,如果上一个没到达本次更新的人不管有没有超时直接算。
  2.当人数并不能排满所有窗口*黄线时,直接更新每个窗口。
AC代码:
#include <stdio.h>
#include <queue>
using namespace std;
struct Node{
	int id;//编号,感觉其实没必要
	int time;//干事的时间
	int window;//在几号窗口
	int hh;//干完事的小时
	int mm;//干完事的分钟
}business[1005];
queue<int> que[25];//窗口队列
int hh[25];//每个窗口当前时间
int mm[25];
int n, m, k, q, p_num;//p_num为加了几个人
void update(int i)//更新信息,i为窗口数
{
	int temp = que[i].front();
	if(hh[i] >= 17)//当此时该窗口第一个人刚刚开始(还没有开始)办理业务的时间,如果达到了17点或者超过17点就都是sorry,把hh赋值为100方便判断
	{
		hh[i] = 100;
		business[temp].hh = 100;
	}
	else//否则更新他干完事的时间,以及他离开队列时的时间也就是下一个人刚刚开始(还没有开始)办理业务的时间,因为hh[i]和mm[i]也被更新了,所以队列的时间也能更新
	{
		mm[i] += business[temp].time;
		hh[i] += mm[i] / 60;
		mm[i] %= 60;
		business[temp].hh = hh[i];
		business[temp].mm = mm[i];
	}
}
int mintime()//返回所有窗口干完每个窗口的第一个人的业务后,此时的最小时间,返回窗口号
{
	int minp = 0x3fffffff, mini;
	for(int i = 1; i <= n; i++)
	{
		int timei = hh[i] * 60 + mm[i];
		if(timei < minp)
		{
			minp = timei;
			mini = i;
		}
	}
	return mini;
}
int main(void)
{
	int at_which_windows = 0;//排黄线时的窗口(因为排黄线时得按顺序从1~n
	int query;//询问次数
	scanf("%d %d %d %d", &n, &m, &k, &q);
	fill(hh + 1, hh + n + 1, 8);
	for(int i = 1; i <= m * n; i++)
	{
		scanf("%d", &business[i].time);
		business[i].id = i;
		if(at_which_windows == n)//窗口循环从n回到1
			at_which_windows = 0;
		at_which_windows++;
		business[i].window = at_which_windows;
		que[at_which_windows].push(i);
		p_num++;
		if(p_num >= k)//人数排不满黄线则退出
			break;
	}
	for(int i = 1; i <= n; i++)//先更新每一个窗口搞完第一个人的时间
	{
		update(i);
	}
	while(p_num < k)//如果有人还在黄线外
	{
		int mint = mintime();//找到最快能结束第一个人业务的窗口
		que[mint].pop();//第一个人结束了业务,此时其他窗口满人(一开始排满了黄线,所有窗口都满了人,现在结束了一个人的业务,其他窗口肯定是满的,此时本着人最少的原则,
//把线外的第一个人放到刚刚出队列的那个队列去,只要有人还在黄线外,则不存在除了刚刚出队列的队列外的队列人没排满的情况,因为只要线外有人,哪一个没满就会补上,如果同时结束,窗口编号小的才会出队列,
//而大的要等到下一次寻找最小时间时才会出队列,尽管两者时间相同。 scanf("%d", &business[++p_num].time); business[p_num].id = p_num; business[p_num].window = mint; que[mint].push(p_num); update(mint);//重新更新本次第一个结束的时间,由于其他窗口没动,所以第一结束时间不变 } for(int i = 1; i <= n; i++) { if(!que[i].empty())//为什么先要出队列?因为之前最后一步是更新队列,实际上已经计算了第一个人出队列的时间,如果不出队列,则会重复计算,该队列的时间会有误 que[i].pop(); while(!que[i].empty()) { update(i);//之后就是更新在出队列,因为我们想要更新该id的办完业务的信息 que[i].pop(); } } for(int i = 0; i < q; i++) { scanf("%d", &query); if(business[query].hh != 100) printf("%02d:%02d\n", business[query].hh, business[query].mm); else printf("Sorry\n"); } return 0; }

 

 

 

 

 
posted @ 2019-11-23 17:48  funforever  阅读(285)  评论(0编辑  收藏  举报