PTA甲级 1014 Waiting in Line (30point(s))

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南		//这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

在这里插入图片描述

题目原文

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 (N**M+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 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, custome**r1 is served at windo**w1 while custome**r2 is served at windo**w2. Custome**r3 will wait in front of windo**w1 and custome**r4 will wait in front of windo**w2. Custome**r5 will wait behind the yellow line.

At 08:01, custome**r1 is done and custome**r5 enters the line in front of windo**w1 since that line seems shorter now. Custome**r2 will leave at 08:02, custome**r4 at 08:06, custome**r3 at 08:07, and finally custome**r5 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个人.现在有K位客户需要服务.每位客户的服务时长以知.假设所有客户均在8点按客户编号次序等在黄线外.如果有窗口的排队人数没有排满,(没有达到M人)当前在黄线外的第一个客户就会选择这样的窗口中排队人数最少的窗口去排队(排队人数相同时).选择窗口序号最小的窗口去排队.给出Q个查询.每个查询给出一位客户的编号.输出这位客户的服务结束时间.

如果一个客户在17之后,还没有被服务.则不再服务.输出Sorry.而如果一个客户在17点之前被服务,那么无论他的服务时长有多长,都会接受完整服务.

代码如下:

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int maxNode = 1111;
int n, m, k, query, q;
int convertToMinute(int h, int m) {
	return h * 60 + m;				//将时间单位转换位min,方便时间处理
}
struct Window {
	//窗口当前队伍的最后服务时间,队首客户的服务结束时间
	int endTime, popTime;
	queue<int> q;					//队列
}window[20];
int ans[maxNode], needTime[maxNode];//服务结束时间和服务需要时间
int main(){
	int inIndex = 0;				//当前第一个未入队的客户编号
	//窗口数,窗口人数上限.客户数,查询数
	scanf("%d%d%d%d", &n, &m, &k, &query);
	for (int i = 0; i < k; ++i) {
		scanf("%d", &needTime[i]);	//读入服务器需要时间
	}
	for (int i = 0; i < n; ++i) {
		window[i].popTime = window[i].endTime = convertToMinute(8,0);
	}
	for (int i = 0; i < min(n * m, k); ++i) {	//注意是min(n*m,K)
		//循环入队
		window[inIndex % n].q.push(inIndex);
		//更新窗口的服务结束时间endTime
		window[inIndex % n].endTime += needTime[inIndex];
		//对窗口的第一个客户,更新popTime
		if (inIndex < n)	window[inIndex].popTime = needTime[inIndex];
		//当前入队的客户服务结束时间直接保存作为答案
		ans[inIndex] = window[inIndex % n].endTime;
		++inIndex;
	}
	for (; inIndex < k; inIndex++) {			//处理剩余客户的入队
		int idx = -1, minPopTime = 1 << 30;		//寻找所有窗口的最小popTime
		for (int i = 0; i < n; ++i) {
			if (window[i].popTime < minPopTime) {
				idx = i;
				minPopTime = window[i].popTime;
			}
		}
		//找到最小popTime的窗口编号未idx,下面更新窗口的队列情况
		Window& W = window[idx];
		W.q.pop();					//对首客户离开
		W.q.push(inIndex);			//客户inIndex入队
		W.endTime += needTime[inIndex];		//更新该窗口的队列的endTime
		W.popTime += needTime[W.q.front()];	//更新该窗口的popTime
		//客户inIndex的服务结束时间为该窗口的endTime
		ans[inIndex] = W.endTime;
	}
	for (int i = 0; i < query; ++i) {
		scanf("%d", &q);					//查询客户编号
		if (ans[q - 1] - needTime[q - 1] >= convertToMinute(17, 0)) {
			printf("Sorry\n");				//服务开始时间达到17:00输出Sorry		
		}
		else {								//否则输出服务结束时间
			printf("%02d:%02d\n", ans[q - 1] / 60, ans[q - 1] % 60);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值