假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。
本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。
输入格式:
输入第1行给出正整数N(≤1000),为顾客总人数;随后N行,每行给出一位顾客的到达时间T
和事务处理时间P
,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。这里假设每位顾客事务被处理的最长时间为60分钟。
输出格式:
在第一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。
在第二行中按编号递增顺序输出每个窗口服务了多少名顾客,数字之间用1个空格分隔,行末不能有多余空格。
输入样例:
9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3
输出样例:
6.2 17 61
5 3 1
参考代码:
#include<bits/stdc++.h>
using namespace std;
struct Customers {
int arrived_time;
int need_time;
int wait_time;
};
int counter_time[10] = {0};
int counter_serve[10] = {0};
int main() {
vector<Customers> customers;
int n, k;
cin>>n;
for(int i = 0; i < n; i++) {
Customers t;
cin>>t.arrived_time>>t.need_time;
if(t.need_time>60) t.need_time = 60;
customers.push_back(t);
}
cin>>k;
for(int i = 0; i < n; i++) {
int choose = 0;
for(int j = 0; j < k; j++) {
if(counter_time[choose] > counter_time[j]) choose = j;
if(customers[i].arrived_time >= counter_time[choose]) break;
}
if(customers[i].arrived_time >= counter_time[choose]) {
counter_time[choose] = customers[i].arrived_time + customers[i].need_time;
customers[i].wait_time = 0;
} else {
customers[i].wait_time = counter_time[choose] - customers[i].arrived_time;
counter_time[choose] += customers[i].need_time;
}
counter_serve[choose]++;
}
double sum = 0;
double longest_wait = 0;
double longest_complete = 0;
for(int i = 0; i < n; i++) {
sum += customers[i].wait_time;
if(customers[i].wait_time > longest_wait) longest_wait = customers[i].wait_time;
}
for(int i = 0; i < k; i++) {
if(counter_time[i] > longest_complete) longest_complete = counter_time[i];
}
printf("%.1lf ",sum/n);
cout<<longest_wait<<" "<<longest_complete<<endl;
for(int i = 0; i < k; i++) {
if(i != 0) cout<<" ";
cout<<counter_serve[i];
}
}