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 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, 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 (≤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

申明:

标题虽然声称44行代码AC但代码行数并不是越短越好,而是清晰明了最好,但看了些文章和大佬们的代码量都很大就来分享下自己的方法,并记录下一些思考,我也是改了很多遍才成功的。 

思路: 

首先排队问题可以很自然的想到用队列解决,但不同窗口都有队列那就用vector来表示窗口window装下所有队列。每个顾客都想找当前最短队列排队,且入黄线后就不能换队,那么每次就为顾客找到当前空余的最短队列入队,同时总人数hc++,当hc==n*m时就代表所有队列人数已满,而按题意和生活经验都是最早完成的顾客出队,那就找所有窗口队头最早完成的出队,同时hc--。

最关键的是记录每个顾客的完成时间,当要查询时可以直接找到对应的输出。各人认为是类似于动态规划,记录子问题的解并不断递推。题目中每位顾客的完成时间都是前者的完成时间加上自己的处理时间而与其他无关,就用now数组记录每个窗口当前的完成时间,每完成一个顾客就用他的完成时间覆盖,这样就可避免重复计算。

细节:

17:00截止指的是顾客的开始时间而不是结束时间,若17:00前已经开始,无论时间多久都要服务完,(有点不合理但又很合理。。。)因此最晚开始时间是17:59,那么now记录的当前窗口完成时间>=540时,该顾客就不能被服务。注意当前窗口的now此时就不用更改了,因为后面的顾客都不能再被服务只输出"Sorry"。

测试点2、4、5检测这点,可以用一个大佬的这个例子测试:

测试用例: 

2 1 3 3
1 540 540
1 2 3

测试输出: 

08:01
17:00
17:01
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main(int argc, char** argv) {
	int n, m, k, q, process[1005], query[1005], hc=0, ans, now[21]={0}; //注意now的初始化
	vector<queue<int>> win;
	cin >> n >> m >> k >> q;
	win.resize(n);  //提前开辟窗口大小
	for(int i=1; i<=k; i++) cin >> process[i];
	for(int i=1; i<=k; i++){
		int index=0;
		for(int j=1; j<n; j++){  //为顾客找到最短队列
			if(win[j].size()<m && win[j].size()<win[index].size()) index=j;
		}
		win[index].push(i);
		hc++;
		if(now[index]>=540) query[i]=0;  //不能再被服务
		else{
			query[i]=now[index]+process[i];
			now[index]=query[i];
		}
		if(hc==n*m){  //所有队列满,找到最早完成的顾客出队
			int over=0;
			for(int j=1; j<n; j++){
				if(query[win[j].front()]<query[win[over].front()]) over=j;
			}
			win[over].pop();
			hc--;
		}
	}
	for(int i=0; i<q; i++){
		cin >> ans;
		if(!query[ans]) cout << "Sorry" << endl;
		else printf("%02d:%02d\n", 8+query[ans]/60, query[ans]%60);  //按要求格式输出
	}
	return 0;
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐