1017. Queueing at Bank (25)

来源:互联网 发布:伪原创软件 编辑:程序博客网 时间:2024/05/20 16:35

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 (<=10000) - 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 307:55:00 1617:00:01 207:59:59 1508:01:00 6008:00:00 3008:00:02 208:03:00 10
Sample Output:
8.2


这是一个排队的问题,求用户的平均等待时间。

1.首先输入用户进入的时间和服务需要时间,然后按照时间顺序升序排序;

2.维护每个窗口当前用户完成时的时间,对于某个用户,选择最快完成的窗口(end_time最小,最小时间记录成Min),让该用户使用该窗口。如果用户进入的时间大于Min,说明不用等待,等待时间为0,否则就用Min减去用户进入时间得到等待时间。然后更新该窗口的完成时间,即该用户开始的时间加上花费时间;

3.最后求平均数。注意第二步时如果用户进入时间超过或等于17点,就不用把他计算在内。


代码:

#include <iostream>#include <cstring>#include <vector>#include <cstdlib>#include <cstdio>#include <map>#include <queue>#include <algorithm>using namespace std;#define latest 9*60*60struct customer{string time;int cost;};bool cmp(const customer &c1,const customer &c2){return c1.time<c2.time;}int get_time(string t){int h=atoi(t.substr(0,2).c_str());int m=atoi(t.substr(3,2).c_str());int s=atoi(t.substr(6,2).c_str());return (h-8)*60*60+m*60+s;}int main(){int n,m;cin>>n>>m;vector<customer>customers(n);for(int i=0;i<n;i++){cin>>customers[i].time>>customers[i].cost;customers[i].cost*=60;}sort(customers.begin(),customers.end(),cmp);vector<int>end_time(m,0);vector<int>cost_time(n);int tot_cost=0;int count=0;for(int i=0;i<n;i++){int t=get_time(customers[i].time);if(t>=latest) break;int Min=1<<30,index;for(int j=0;j<m;j++){if(Min>end_time[j]) {Min=end_time[j];index=j;}}if(t<Min) {cost_time[i]=Min-t;end_time[index]=Min+customers[i].cost;}else {cost_time[i]=0;end_time[index]=t+customers[i].cost;}tot_cost+=cost_time[i];count++;}printf("%.1f\n",(tot_cost*1.0)/(60*count));}


0 0
原创粉丝点击