【PAT - 甲级1017】Queueing at Bank (25分)(优先队列,模拟)
題干:
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?(≤10?4??) - 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 10Sample Output:
8.2題目大意:
假設銀行有K個窗口為服務打開。窗戶前面有一條黃線,把等候區分成兩部分。所有的顧客都要在黃線后排隊等候,直到輪到他/她時才有空位。假設沒有一個客戶可以占用一個窗口超過1小時?,F在給定每個客戶的到達時間T和處理時間P(單位是分鐘),你應該告訴所有客戶的平均等待時間。假設沒有兩個客戶同時到達。注意,銀行的營業時間是8點到17點。早到者必須排隊等候至08:00,晚到者(17:00:01或17:00:01以后)不計算在內。
解題報告:
拿一個優先隊列維護可用窗口就可以了,然后因為題意是按照到來的先后順序排隊等待的,所以不需要考慮(7:00:00 25)的顧客和(7:30:00 5)的顧客占用窗口的先后順序問題,因為如果是按照最優方案的話肯定是先安排(7:30:00 5)的顧客更劃算,因為他服務時間短,只有5分鐘。復雜度是O(n*log(k)),如果不用優先隊列的話復雜度是O(n*k),也可以AC。
甚至這題從8:00:00開始一秒一秒的模擬到18:00:00也可以,復雜度是O(36000*k)??梢运伎家幌聻樯兜?8點而不是17點
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int n,k; priority_queue<int,vector<int>,greater<int> > pq; struct Node {int t;int service; } R[MAX]; int hh,mm,ss; bool cmp(Node a,Node b) {return a.t < b.t; } const int ed = 17*3600;//17:00:00 const int st = 8 *3600;// 8:00:00 int main() {cin>>n>>k;for(int i = 1; i<=n; i++) {scanf("%d:%d:%d%d",&hh,&mm,&ss,&R[i].service);R[i].service *= 60;R[i].t = hh*3600 + mm*60 + ss;}sort(R+1,R+n+1,cmp);int wait = 0,all = 0;for(int i = 1; i<=n; i++) {if(R[i].t >= ed) break;all++;if(R[i].t < st) wait += st - R[i].t,R[i].t = st;if(pq.size() < k) pq.push(R[i].t + R[i].service);else {int cur = pq.top();pq.pop();if(cur < R[i].t) pq.push(R[i].t + R[i].service);else {wait += cur - R[i].t;pq.push(cur + R[i].service);}}}printf("%.1f\n",wait/60.0/all);return 0 ; }?
總結
以上是生活随笔為你收集整理的【PAT - 甲级1017】Queueing at Bank (25分)(优先队列,模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【POJ - 1459】Power Ne
- 下一篇: 无条件信用卡申请 无条件信用卡办理