Queueing at Bank
题目描述:
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 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
我的代码如下,可是有一个case过不了,希望各位大神帮忙
#include <iostream>#include <map>#include <iomanip>#include <vector>#include <string>#include <sstream>#include <algorithm>#include <cassert>#include <fstream>using namespace std;int trans(string s){ char a,b; a = s[0]; b = s[1]; int h = (a - '0') * 10 + (b - '0'); a = s[3]; b = s[4]; int m = (a - '0') * 10 + (b - '0'); a = s[6]; b = s[7]; int sec = (a - '0') * 10 + (b - '0'); int time = h * 3600 + m * 60 + sec; return (time - 8 * 3600);}int main(){ unsigned long long total_waiting_time(0); vector<int> window; map<int,int> people; int n,k; cin >> n >> k; for(int i = 0;i < n;++ i) { string arrive_time; int serve_time; cin >> arrive_time >> serve_time; int time = trans(arrive_time); people.insert(make_pair(time,serve_time * 60)); } for(int i = 0;i < k; ++i) window.push_back(0); int now(0); map<int,int>::iterator it; for(it = people.begin();it != people.end();++ it) if (it->first > 9 * 3600) { break; } people.erase(it,people.end()); for(it = people.begin();it != people.end();++ it) if (it->second > 3600) { it->second = 3600; } int x = people.size(); map<int,int>::iterator waiting_head; waiting_head = people.begin(); while(1) { if(now > 9 * 3600) break; if (waiting_head == people.end()) break; for(int i = 0;i < window.size(); ++ i) //对于每个窗口 { if(0 == window[i]) //如果该窗口没有人 { if (waiting_head->first <= now) //如果当前队列里有人 { window[i] = waiting_head->second; total_waiting_time += (now - waiting_head->first); people.erase(waiting_head); waiting_head = people.begin(); if (waiting_head == people.end()) break; } else //如果当前队列里没有人 { int time_dif = waiting_head->first - now; now = waiting_head->first; for(int j = 0;j < window.size();++ j) { window[j] -= time_dif; if(window[j] < 0) window[j] = 0; } i = -1; } } } int min = 0xFFFFFFF; for(int i = 0;i < window.size(); ++ i) //寻找需要最短服务时间的窗口 if (min > window[i]) min = window[i]; now += min; for(int i = 0;i < window.size();++ i) //等待需要最短服务时间的窗口完成 { window[i] -= min; } } double r; if(x == 0) r = 0; else r = total_waiting_time / ((double) x * 60.0); cout << fixed << setprecision(1) << r;}
if(now > 9 * 3600) break;