强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
还有就是非常非常有用的 算法笔记 全名是
算法笔记 上级训练实战指南 //这本都是PTA的题解
算法笔记
PS 今天也要加油鸭
题目原文
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.
Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤104) - the total number of customers, and K (≤100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS
- the arriving time, and P - the processing time in minutes of a customer. Here HH
is in the range [00, 23], MM
and SS
are both in [00, 59]. It is assumed that no two customers arrives at the same time.
Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.
Output Specification:
For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.
Sample Input:
7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10
Sample Output:
8.2
题目大意:
有N个客户,K个窗口,给出每个客户的到达时间和服务时长.如果所有窗口都被占用.那么客户将在黄线外进行排队(只有一个对);否则.如果有窗口无人服务.那么由队列的第一个客户前往接受服务.求客户的平均等待时长.注意:银行开放时间为
08:00-17:00,在8点前到达的需要等待,在17点之后还没有被服务的客户,请明天再来.超过1H的服务时长,会被缩短到1H
代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int K = 111;
const int INF = 1000000000;
struct Customer {
int comeTime, serveTime; //客户到达的时间及服务时长
}newCustomer; //newCustomer临时存放新客户的信息
vector<Customer> custom; //模拟队列
int convertTime(int h, int m, int s) {
return h * 3600 + m * 60 + s; //将时间转换为以s为单位,方便比较和计算
}
bool cmp(Customer a, Customer b) { //将客户到达的时间排序
return a.comeTime < b.comeTime;
}
int endTime[K]; //endTime[K]记录了i号窗口的当前服务客户的结束时间
int main(void) {
int c, w, totTime = 0; //totTime记录总等待时长
int stTime = convertTime(8, 0, 0); //开门时间是8点
int edTime = convertTime(17, 0, 0); //关门时间是17点
scanf("%d%d", &c, &w); //客户数,窗口数
for (int i = 0; i < w; ++i) endTime[i] = stTime; //没有客户,初始化为stTime
for (int i = 0; i < c; ++i) {
int h, m, s, serveTime; //时,分,秒,服务时长
scanf("%d:%d:%d %d", &h, &m, &s, &serveTime);
int comeTime = convertTime(h, m, s);
if (comeTime > edTime) continue; //超过关门时间,不被计算
newCustomer.comeTime = comeTime;
newCustomer.serveTime = serveTime <= 60 ? serveTime * 60 : 3600;
custom.push_back(newCustomer);
}
sort(custom.begin(), custom.end(), cmp); //按到达时间从先到后排序
for (int i = 0; i < custom.size(); ++i) {
int idx = -1, minEndTime = INF;
for (int j = 0; j < w; ++j) {
if (endTime[j] < minEndTime) {
minEndTime = endTime[j];
idx = j;
}
}
//idx为最早服务结束的窗口编号,将客户custom[i]分配到该窗口
if (endTime[idx] <= custom[i].comeTime) {
//如果客户custom[i]在窗口idx空闲之后才来.则直接接受服务
endTime[idx] = custom[i].comeTime + custom[i].serveTime;
}
else { //如果客户custom[i]来得太早,则需等待,等待时间计入totTime
totTime += (endTime[idx] - custom[i].comeTime);
endTime[idx] += custom[i].serveTime;
}
}
if (custom.size() == 0) printf("0.0");
else printf("%.1f", totTime / 60.0 / custom.size());
}