Description
假设银行有 K
个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。
本题要求输出前来等待服务的 N
位顾客的平均等待时间、最长等待时间、最后完成时间。
Input
输入第1行给出正整数 N
(≤1000)
,为顾客总人数;
随后 N
行,每行给出一位顾客的 到达时间T 和 事务处理时间P
,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数 K
(≤10)
,为开设的营业窗口数。
Output
在一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。
Sample
Input
Output
9 0 20 1 15 1 61 2 10 10 5 10 3 30 18 31 25 31 2 3 6.2 17 62
代码解释:全部都写在代码里
#include <iostream>
#include <queue>
#include <iomanip>
using namespace std;
/*
假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。
当有窗口空闲时,下一位顾客即去该窗口处理事务。
当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。
本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间。
1.顾客类定义,来的时刻 和 处理时长
2.窗口类定义,是否被占用,被占用的时刻,被占用时长
*/
class customer //顾客类定义
{
public:
int come; //来到时刻
int occ; //处理时长
customer(int c, int o) : come(c), occ(o) {}
};
class win //窗口类定义
{
public:
bool busy; //是否被占用
int come_time; //被占用的时刻
int occ_time; //被占用时长
win() : busy(false), come_time(0), occ_time(0) {}
};
int main()
{
int n, k;
int totalWaitTime = 0; //总等待时间,用于计算平均等待时间
int waitTime = 0; //每位顾客所需要等待到有窗口的时间
int waitMax = 0; //记录最长的等待时间
queue<customer> cus; //顾客队列
cin >> n;
//输入顾客信息
for (int i = 0; i < n; i++)
{
int temp1, temp2;
cin >> temp1 >> temp2;
cus.push(customer(temp1, temp2));
}
cin >> k;
win W[k]; //窗户数组
while (!cus.empty())
{
int check = 0; // 检测顾客是否已经获取了窗口
int timeCompare = 0; // 用于比较当前等待时间
for (int j = 0; j < k; j++)
{
// 如果窗口被占用,更新窗口信息-
if (W[j].busy)
{
//计算 (窗口的被占用时长 -(顾客来到时刻 - 窗口被占用时刻)),即当前顾客需要等待该窗口变为空闲的时间
timeCompare = W[j].occ_time - (cus.front().come - W[j].come_time);
if (timeCompare <= 0) //如果等待时间小于0,窗口变为空闲状态
{
W[j].busy = false;
}
// 如果窗口不能变为空闲状态,获取更新顾客真正需要等待的时长
if (j == 0 || waitTime > timeCompare)
{
waitTime = timeCompare;
}
}
// 如果有空闲窗口,立刻占用
if (!W[j].busy && !check)
{
W[j].busy = true;
W[j].come_time = cus.front().come;
W[j].occ_time = cus.front().occ;
cus.pop();
// 检测顾客已获取窗口
check = 1;
}
}
// 顾客未能一来就有窗口
if (!check)
{
// 更新最长等待时间和等待总时长
totalWaitTime += waitTime;
if (waitTime > waitMax){
waitMax = waitTime;
}
// 顾客跳到等待时间之后
cus.front().come += waitTime;
}
}
// 所有顾客轮上后,获取最后的窗口处理时间,计算最后完成时间(最晚顾客用上窗口的时刻 + 到结束所需要的时长)
// 获取最晚的顾客用上窗口的时刻
int lastComeTime = 0;
for (int j = 0; j < k; j++)
{
if (W[j].busy && W[j].come_time > lastComeTime)
{
lastComeTime = W[j].come_time;
}
}
// 获取到结束所需的时间
int lastLongest = 0;
int temp;
for (int j = 0; j < k; j++)
{
if (W[j].busy)
{
temp = (W[j].occ_time - (lastComeTime - W[j].come_time));
if (temp > lastLongest)
{
lastLongest = temp;
}
}
}
cout << fixed << setprecision(1) << totalWaitTime / (float)n << " " << waitMax << " " << lastLongest + lastComeTime;
return 0;
}