PAT1017. 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
思路:让所有人排成一条线,然后进行窗口的枚举 寻找到到哪个窗口结束时间最短。 思路理清非常好。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 #define MAX 10010
 7 struct Person
 8 {
 9     int arrive;
10     int serve;
11 }Per[MAX];
12 queue<Person> Q;
13 bool cmp(Person A,Person B)
14 {
15     return A.arrive<B.arrive;
16 }
17 int Convert(int hour,int min,int sec)
18 {
19     return hour*3600+min*60+sec;
20 }
21 int Endtime[110];
22 int main()
23 {
24     int N,M;
25     int open=Convert(8,0,0);
26     int close=Convert(17,0,1);
27     scanf("%d%d",&N,&M);
28     for(int i=0;i<M;i++)
29       Endtime[i] = open;
30     for(int i=0;i<N;i++)
31     {
32         int hour,min,sec,wait;
33         scanf("%d:%d:%d %d",&hour,&min,&sec,&wait);
34         wait=wait<=60?wait*60:3600;
35         int arrive=Convert(hour,min,sec);
36         int serve=Convert(0,0,wait);
37         Per[i].arrive=arrive;
38         Per[i].serve=serve;
39     }
40     sort(Per,Per+N,cmp);
41     int ans=0;
42     for(int i=0;i<N;i++)
43     {
44         if(Per[i].arrive<close)
45         {
46             Q.push(Per[i]);
47         }
48         else
49             break;
50     }
51     int size=Q.size();
52     while(!Q.empty())
53     {
54         Person tem=Q.front();
55         Q.pop();
56         int min=0xfffffff,pt=-1;
57         //选择结束最早的
58         for(int i=0;i<M;i++)
59         {
60             if(Endtime[i]<min)
61             {
62                 min=Endtime[i];
63                 pt=i;
64             }
65         }
66         if(Endtime[pt]<=tem.arrive)
67         {
68             Endtime[pt]=tem.arrive+tem.serve;
69         }
70         else
71         {
72
73             ans+=Endtime[pt]-tem.arrive;
74             Endtime[pt]+=tem.serve;
75         }
76     }
77     printf("%.1f\n",ans/60.0/size);
78     return 0;
79 }

时间: 2024-10-07 19:05:12

PAT1017. Queueing at Bank的相关文章

pat1017. Queueing at Bank (25)

1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 custo

PAT-1017 Queueing at Bank (25)

这道题目是一道模拟题目,题目意思是有n个串口,和一串顾客到达的时间,顾客按先来先服务方式排队,问你这些顾客的平均等待时间是多少?实现:首先把顾客到达顺序记录下来,然后依据到达时间进行排序,k个窗口维护一个数据结构,就是服务的结束时间last,刚开始用end变量,发现提交的时候报错了,估计是系统保留的关键字.每一次从记录里面拿一个人,遍历k个窗口,查看谁的结束时间最早,然后把这个顾客加到这个窗口上,更新结束时间.如此知道最后,分享一下我的测试数据: 7 3 07:55:00 16 17:00:01

PAT-甲级-1017. Queueing at Bank【模拟】

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 tur

PAT 1017 Queueing at Bank[一般]

1017 Queueing at Bank (25)(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

1017 Queueing at Bank (25 分)

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/h

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 ther

1017. Queueing at Bank (25) - priority_queuet

题目如下: 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 an

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 ther

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 ther