1017. Queueing at Bank (25)
2016-07-04 14:24
351 查看
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:
Sample Output:
这是一个排队的问题,求用户的平均等待时间。
1.首先输入用户进入的时间和服务需要时间,然后按照时间顺序升序排序;
2.维护每个窗口当前用户完成时的时间,对于某个用户,选择最快完成的窗口(end_time最小,最小时间记录成Min),让该用户使用该窗口。如果用户进入的时间大于Min,说明不用等待,等待时间为0,否则就用Min减去用户进入时间得到等待时间。然后更新该窗口的完成时间,即该用户开始的时间加上花费时间;
3.最后求平均数。注意第二步时如果用户进入时间超过或等于17点,就不用把他计算在内。
代码:
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 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
这是一个排队的问题,求用户的平均等待时间。
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*60 struct 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)); }
相关文章推荐
- 在命令行用 sort 进行排序
- Linux Shell - 如何使用sort与uniq命令删除重复的文本行
- Redis sort 排序命令详解
- 批处理命令教学之字符串排序(sort)
- mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法
- C++ 关于STL中sort()对struct排序的方法
- C#中Arraylist的sort函数用法实例分析
- Js中sort()方法的用法
- js采用concat和sort将N个数组拼接起来的方法
- 如何写JS数组sort的比较函数
- javascript中sort()的用法实例分析
- 详解Matlab中 sort 函数用法
- js模拟实现Array的sort方法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- js表数据排序 sort table data
- javascript 数组排序函数sort和reverse使用介绍
- JavaScript Table排序 2.0 (更新)
- js中数组(Array)的排序(sort)注意事项说明
- Javascript数组的排序 sort()方法和reverse()方法
- Java数据结构及算法实例:插入排序 Insertion Sort