PAT 1017

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 (<=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 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 typedef struct Customer{
 6     int arrivingTime,processTime;
 7 }Customer;
 8 
 9 Customer customer[10000];
10 int reminderTime[10000];
11 int serveringNum[100];
12 int cmp(const Customer&,const Customer&);
13 
14 int main()
15 {
16     int N,K,needServerNum;
17     int hour,min,sec,pTime,time;
18     int i;
19     const int earliestTime = 8 * 60 * 60;
20     const int latestTime = 17 * 60 * 60;
21     int yellowLineNum;
22     while(scanf("%d%d",&N,&K) != EOF){
23         needServerNum =  0;
24         for(i=0;i<N;++i){
25             scanf("%d:%d:%d %d",&hour,&min,&sec,&pTime);
26             time = hour * 60 * 60 + min * 60 + sec;
27             if(time > latestTime)
28                 continue;
29             customer[needServerNum].arrivingTime = time;
30             customer[needServerNum++].processTime = pTime * 60;
31         }
32         sort(customer,customer+needServerNum,cmp);
33         for(i=0;i<needServerNum;++i)
34             reminderTime[i] = customer[i].processTime;
35         yellowLineNum = 0;
36         double totalServerTime = 0.0;
37         int serveredNum = 0;
38         for(i=0;i<K;++i){
39             serveringNum[i] = -1;
40             if(yellowLineNum < needServerNum && customer[yellowLineNum].arrivingTime <= earliestTime){
41                 serveringNum[i] = yellowLineNum;
42                 totalServerTime += earliestTime - customer[yellowLineNum++].arrivingTime;
43                 ++serveredNum;
44             }
45         }
46         int nowTime = earliestTime;
47         while(serveredNum < needServerNum){
48             int minWin = -1;
49             int num,hasWin = 0;
50             for(i=0;i<K;++i){
51                 num = serveringNum[i];
52                 if(num != -1 && (minWin == -1 || minWin > reminderTime[num]))
53                     minWin = reminderTime[num];
54                 else if(num == -1){
55                     hasWin = 1;
56                 }
57             }
58             if((minWin == -1) || (hasWin && (nowTime + minWin) > customer[yellowLineNum].arrivingTime)){
59                 minWin = customer[yellowLineNum].arrivingTime - nowTime;
60                 nowTime = customer[yellowLineNum].arrivingTime;
61             }
62             else
63                 nowTime += minWin;
64             for(i=0;i<K;++i){
65                 int num = serveringNum[i];
66                 if(num != -1){
67                     reminderTime[num] -= minWin;
68                     if(reminderTime[num] == 0){
69                         if(yellowLineNum < needServerNum && customer[yellowLineNum].arrivingTime <= nowTime){
70                             serveringNum[i] = yellowLineNum;
71                             totalServerTime += nowTime - customer[yellowLineNum++].arrivingTime;
72                             ++serveredNum;
73                         }
74                         else{
75                             serveringNum[i] = -1;
76                         }
77                     }
78                 }
79                 else if(yellowLineNum < needServerNum && customer[yellowLineNum].arrivingTime <= nowTime){
80                     serveringNum[i] = yellowLineNum;
81                     totalServerTime += nowTime - customer[yellowLineNum++].arrivingTime;
82                     ++serveredNum;
83                 }
84             }
85         }
86         printf("%.1lf\n",totalServerTime / (needServerNum * 60));
87     }
88     return 0;
89 }
90 
91 int cmp(const Customer &a,const Customer &b)
92 {
93     return a.arrivingTime < b.arrivingTime;
94 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值