pat1017. 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/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 10Sample 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 }