PAT1017:Queueing at Bank
1017. Queueing at Bank (25)
時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, YueSuppose 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.
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 10Sample Output:
8.2
思路
貪心策略,將辦理業務的人按到達時間遞增排序,先來的優先服務(將時間統一轉換為秒方便計算)。
註意:
1.業務辦理時間不應超過1小時。
2.17點之後來的人不提供服務。
3.一個窗口的空閑時刻有兩種情況:
1)空閑直到下一個人customer[i]到達銀行辦理業務,那麽這個窗口的下一個空閑時刻為customer[i]的到達時間加上他辦理業務需要的時間。
2)如果在一個窗口空閑前,custmoer[i]就先來了,那麽他需要等(窗口空閑時間-他到達的時間)這麽一段時間。該窗口的下一個空閑時刻就是它當前空閑時刻加上customer[i]的業務辦理時間。
代碼
#include<iostream> #include<vector> #include<algorithm> #include<iomanip> using namespace std; class person { public: int cometime; int taketime; person(int ct,int tt) { cometime = ct; taketime = tt; } }; /*貪心策略 先來的先服務,有空窗口就服務*/ bool cmp(const person& a,const person& b) { return a.cometime < b.cometime; } int main() { int N,K; vector<person> customer; while(cin >> N >> K) { vector<int> windows(K,28800); for(int i = 0;i < N;i++) { int hh,mm,ss,lasttime; scanf("%d:%d:%d %d",&hh,&mm,&ss,&lasttime); if(lasttime > 60) lasttime = 60; lasttime *= 60; int arrivetime = hh * 3600 + mm * 60 + ss; if(arrivetime > 61200) continue; customer.push_back(person(arrivetime,lasttime)); } sort(customer.begin(),customer.end(),cmp); double waitTime = 0; for(int i = 0;i < customer.size();i++) { int minwindow = windows[0],tmpindex = 0; for(int j = 0;j < K;j++) { if(windows[j] < minwindow) { minwindow = windows[j]; tmpindex = j; } } if(customer[i].cometime >= minwindow) { windows[tmpindex] = customer[i].cometime + customer[i].taketime; } else { waitTime += minwindow - customer[i].cometime; windows[tmpindex] += customer[i].taketime; } } if(customer.empty()) cout << "0.0" << endl; else cout << fixed << setprecision(1) << waitTime/60.0/customer.size() << endl; } }
PAT1017:Queueing at Bank