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 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<set>
  2 #include<map>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<queue>
  8 #include<vector>
  9 #include<cmath>
 10 using namespace std;
 11 #define open 28800
 12 #define close 61200
 13 struct custom{
 14     int come,cost,finish;
 15 };
 16 void swap(custom &a,custom &b){
 17     custom c=a;
 18     a=b;
 19     b=c;
 20 }
 21 void BuildHeap(custom *cc,int m){
 22     int fa,child=m-1,i;
 23     for(i=(child-1)/2;i>=0;i--){
 24         child=i*2+1;//左儿子
 25         for(fa=i;child<m;child=fa*2+1){
 26             if(child+1<m&&cc[child].finish>cc[child+1].finish){
 27                 child++;
 28             }
 29             if(cc[child].finish<cc[fa].finish){
 30                 swap(cc[fa],cc[child]);
 31                 fa=child;
 32             }
 33             else{
 34                 break;
 35             }
 36         }
 37     }
 38 }
 39 void Insertion(custom *cc,custom cur,int &m){
 40     int i=m++;
 41     for(;i>0&&cc[(i-1)/2].finish>cur.finish;i=(i-1)/2){
 42         cc[i]=cc[(i-1)/2];
 43     }
 44     cc[i]=cur;
 45 }
 46 custom DeleteMin(custom *cc,int &m){
 47     custom cur=cc[0];
 48     custom temp=cc[--m];
 49     int fa,child=1;
 50     for(fa=0;child<m;child=fa*2+1){
 51         if(child<m-1&&cc[child].finish>cc[child+1].finish){
 52             child++;
 53         }
 54         if(cc[child].finish<temp.finish){
 55             cc[fa]=cc[child];
 56             fa=child;//保证fa指向当前要比较的节点
 57         }
 58         else{
 59             break;
 60         }
 61     }
 62     cc[fa]=temp;
 63     return cur;
 64 }
 65 bool cmp(custom a,custom b){
 66     return a.come<b.come;
 67 }
 68 int main(){
 69     //freopen("D:\\input.txt","r",stdin);
 70     int n,nn;
 71     int i,j;
 72     scanf("%d %d",&n,&nn);
 73
 74     //cout<<n<<" "<<nn<<endl;
 75
 76     custom *c=new custom[n+5],*cc=new custom[nn+5];
 77     int h,m,s,cost;
 78     for(i=0;i<n;i++){
 79         scanf("%d:%d:%d %d",&h,&m,&s,&cost);
 80         c[i].come=h*3600+m*60+s;
 81         c[i].cost=cost*60;
 82     }
 83     int totaltime=0,count=0;
 84     sort(c,c+n,cmp);
 85
 86
 87     j=1;
 88     for(i=0;i<nn&&i<n;i++){
 89         if(c[i].come<open){
 90             totaltime+=open-c[i].come;
 91             c[i].come=open;
 92         }
 93         if(c[i].come>close){
 94             break;
 95         }
 96         c[i].finish=c[i].come+c[i].cost;
 97         cc[i]=c[i];
 98         count++;
 99     }
100
101     //cout<<count<<endl;
102
103     if(count<nn){//人数不够
104         printf("%.1lf\n",totaltime*1.0/60/count);//不经意间看到,让我找了将近一小时!!
105         return 0;
106     }
107
108     BuildHeap(cc,count);//建堆
109
110
111     custom cur;
112     for(;i<n;i++){
113         cur=DeleteMin(cc,nn);
114         if(c[i].come<=close){//cur.finish<=close&&
115             if(cur.finish<c[i].come){
116                 c[i].finish=c[i].come+c[i].cost;
117             }
118             else{
119                 c[i].finish=cur.finish+c[i].cost;
120                 totaltime+=cur.finish-c[i].come;
121             }
122             cur=c[i];
123             Insertion(cc,cur,nn);
124             count++;
125         }
126         else{
127             break;
128         }
129     }
130     printf("%.1lf\n",totaltime*1.0/count/60);
131     return 0;
132 }
时间: 2024-11-06 07:33:48

pat1017. Queueing at Bank (25)的相关文章

PAT-1017 Queueing at Bank (25)

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

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

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

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

PAT甲题题解-1017. Queueing at Bank (25)-模拟

有n个客户和k个窗口,给出n个客户的到达时间和需要的时长有空闲的窗口就去办理,没有的话就需要等待,求客户的平均时长.如果在8点前来的,就需要等到8点.如果17点以后来的,则不会被服务,无需考虑. 按客户的到达时间排序建立一个优先级队列,一开始放入k个窗口,初始结束时间为8*3600然后for循环客户,每次从优先级队列中取出最早结束时间的窗口如果客户比结束时间来的早,就需要等待如果客户比结束时间来的晚,就无需等待最后只要统计那些到达时间在17*3600之前的客户即可. #include <iost

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

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