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 }