【学习笔记】〖九度OJ〗题目1326:Waiting in Line

题目1326:Waiting in Line

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:190

解决:53

题目描述:

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

输入:

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.

输出:

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.

样例输入:
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
样例输出:
08:07
08:06
08:10
17:00
Sorry
答疑:
解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-8049-1-1.html

本题在浙大PAT上有同样的题目,但两个OJ对题目的解读并不同,区别在于对


“for those customers who cannot be served before 17:00, you must output


 ‘Sorry’ instead.”这句话的理解。


九度要求对于17点之前未处理完成的,一律为sorry。


浙大PAT上要求17:00之前如果开始处理,那么要给出处理完的时间。而对于还在排队


的,均为sorry。


下面是浙大PAT上AC的代码,九度上只需要稍微改动一下,大体思想相同,感觉类似CPU调度时的时间片轮转算法,

排队就是简单的FIFO。


#include<iostream>
#include<cstdio>
using namespace std;

class Lineitem
{
public:
	int trans;
	int no;
};

Lineitem line[22][12];

int cus[1002];
int res[1002];

int main()
{
	freopen("input.in", "r", stdin);
	freopen("output.out","w", stdout);
	int n, m, k, q, i, j;

	int cusP = 0,time=0;

	cin >> n >> m >> k >> q;

	for (i=0; i<k; i++)
	{
		cin >> cus[i];
	}

	//队伍排满
	for (j=0; j<m; j++)
	{
		
		for (i=0; i<n; i++)
		{
			if (cusP>=k)//队伍无人
			{
				line[i][j].no = -1;
				line[i][j].trans = 0;
			}
			else
			{
				line[i][j].no = cusP;
				line[i][j].trans = cus[cusP++];
			}
		}
	}
	for (time=1; time<540; time++)
	{
		for (i=0; i<n; i++)
		{
			//本队无客户,去下一队
			if (line[i][0].no == -1)
			{
				continue;
			}

			line[i][0].trans--;//每个窗口同步服务当前客户
			//客户服务完毕,下一客户进队
			if (line[i][0].trans == 0)
			{
				res[line[i][0].no] = time;//记录当前客户完成时间
				//依次前移
				for (j=0; j<m-1; j++)
				{
					line[i][j] = line[i][j+1];
				}
				
				//新客户入队
				if (cusP<k)
				{
					line[i][j].no = cusP;
					line[i][j].trans = cus[cusP++];
				}
				else
				{
					//无新客户
					line[i][j].no = -1;
					line[i][j].trans = 0;
				}
			}
		}//end of for i
	}//end of for time
	//时间到,还在处理的处理完
	for (i=0; i<n; i++)
	{
		if (line[i][0].no != -1)
		{
			res[line[i][0].no] = time+line[i][0].trans-1;
		}
	}
	//还在队内sorry
	for (i=0; i<n; i++)
	{
		for (j=1; j<m; j++)
		{
			if (line[i][j].no==-1)
			{
				break;
			}
			res[line[i][j].no] = -1;
		}
	}//*/
	//还在黄线外sorry
	while (cusP<k)
	{
		res[cusP++] = -1;
	}

	int index;
	for (i=0; i<q; i++)
	{
		cin >> index;
		index--;
		if (res[index] == -1||n==0||m==0)
		{
			printf("Sorry\n");
		}
		else
		{
			printf ("%02d:%02d\n", res[index]/60+8, res[index]%60);
		}
	}
	return 0;
}


posted @ 2014-03-07 22:23  神枪打麦手  阅读(239)  评论(0编辑  收藏  举报