PTA-1017——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 (≤) - the total number of customers, and K (≤) - 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<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int k,n;
 6 struct Customer{
 7     int need;    //需要的时间 
 8     int arrive;    //到达的时间 
 9     int wait;    //等待的时间 
10 }customer[10001];    //客户信息 
11 int now[101];    //窗口当前时间 
12 int win[101];    //窗口当前客户编号 
13 
14 bool cmp(Customer a,Customer b){    //根据到达时间先后快排 
15     if(a.arrive<b.arrive){
16         return true;
17     }else{
18         return false;
19     }
20 }
21 
22 int main(){
23     cin>>n>>k;
24     int num=1;    //客户数量 
25     memset(customer,0,sizeof(customer));
26     memset(win,0,sizeof(win));
27     for(int i=1;i<=n;i++){
28         int hour,minute,second,time;
29         scanf("%d:%d:%d %d",&hour,&minute,&second,&time);
30         if(time>60){
31             time=60;
32         }
33         int arriveTime=hour*60*60+minute*60+second;
34         if(arriveTime>17*60*60){    //如果到达时间晚于17点,忽略不计 
35             continue;
36         }else{
37             customer[num].arrive=arriveTime;
38             customer[num].need=time*60;        //统一化成秒 
39             num++;
40         }
41     }
42     num--;    //因为是从1开始计的,最终数量要减1 
43     sort(customer,customer+num,cmp);
44     for(int i=1;i<=num;i++){    //如果早于8点,起始等待时间设定为开门前的等待时间,到达时间改为八点 
45         if(customer[i].arrive<8*60*60){
46             customer[i].wait=8*60*60-customer[i].arrive;
47             customer[i].arrive=8*60*60;    
48         }
49     }
50     for(int i=1;i<=num;i++){
51         if(i<=k){        //每个窗口的第一个客户 
52             win[i]=i;
53             now[i]=customer[i].arrive;    //每个窗口的初始时间是最早进入的客户的到达时间 
54         }else{
55             int it=0;    //最早结束的窗口 
56             int finish=10000000;    //最早结束的时间 
57             for(int j=1;j<=k;j++){    //寻找最早结束的窗口 
58                 if(now[j]+customer[win[j]].need<finish){
59                     finish=now[j]+customer[win[j]].need;
60                     it=j;
61                 }
62             }
63             win[it]=i;
64             customer[i].wait+=max(finish-customer[i].arrive,0);    //等待时间最小为0,避免出现等待时间为负数的情况 
65             now[it]=max(finish,customer[i].arrive); 
66         }
67     }
68     float ans=0.0;
69     for(int i=1;i<=num;i++){
70         ans+=customer[i].wait;
71     }
72     if(num!=0){
73         ans/=num*60;    //最终结果不是秒,是分钟 
74     }
75     printf("%.1f",ans);
76     return 0;
77 } 
 

猜你喜欢

转载自www.cnblogs.com/orangecyh/p/10313748.html