http://pat.zju.edu.cn/contests/pat-practise/1017
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 int N,K,wait_time=0; 7 struct Customer 8 { 9 int arrive_time; 10 int need_time; 11 }; 12 13 struct Customer customer[10002]; 14 15 struct Windows 16 { 17 int next_available_time; 18 }; 19 20 struct Windows windows[102]; 21 22 bool cmp(struct Customer a,struct Customer b) 23 { 24 return a.arrive_time<b.arrive_time; 25 } 26 27 int find_available_windows(int arrive_time) 28 { 29 int i; 30 for(i=0;i<K;i++) { 31 if(windows[i].next_available_time<=arrive_time) { 32 return i; 33 } 34 } 35 return -1; 36 } 37 38 int find_earliest_window() 39 { 40 int i; 41 int e=0; 42 for(i=1;i<K;i++) { 43 if(windows[i].next_available_time<windows[e].next_available_time) { 44 e=i; 45 } 46 } 47 return e; 48 } 49 50 int main() 51 { 52 scanf("%d%d",&N,&K); 53 int i; 54 char arrive_time[20]; 55 int need_time; 56 for(i=0;i<K;i++) 57 windows[i].next_available_time=3600*8; 58 int len=0; 59 for(i=0;i<N;i++) { 60 int h,m,s; 61 scanf("%s%d",arrive_time,&need_time); 62 if(strcmp(arrive_time,"17:00:00")>0) 63 continue; 64 65 sscanf(arrive_time,"%d:%d:%d",&h,&m,&s); 66 if(h<8) 67 wait_time+=8*3600-(3600*h+60*m+s); 68 customer[len].arrive_time=3600*h+60*m+s; 69 customer[len++].need_time=need_time*60; 70 } 71 N=len; 72 73 sort(customer,customer+N,cmp); 74 75 for(i=0;i<N;i++) { 76 int w=find_available_windows(customer[i].arrive_time); 77 if(w>=0) {//找到空闲窗口 78 // windows[w].next_available_time=customer[i].arrive_time+customer[i].need_time; 79 if(customer[i].arrive_time<8*3600) { 80 windows[w].next_available_time=8*3600+customer[i].need_time; 81 } else { 82 windows[w].next_available_time=customer[i].arrive_time+customer[i].need_time; 83 } 84 } else { //找不到空闲窗口 85 w=find_earliest_window(); 86 /* wait_time+=windows[w].next_available_time-customer[i].arrive_time; 87 * windows[w].next_available_time=(windows[w].next_available_time-customer[i].arrive_time)+customer[i].need_time; 88 */ 89 if(customer[i].arrive_time<8*3600) {//如果到得早 窗口的下个可用时间等于当前下个可用时间加新来顾客所需要服务时间 90 wait_time+=windows[w].next_available_time-8*3600; 91 windows[w].next_available_time=windows[w].next_available_time+customer[i].need_time; 92 } else { 93 wait_time+=windows[w].next_available_time-customer[i].arrive_time; 94 windows[w].next_available_time=windows[w].next_available_time+customer[i].need_time; 95 } 96 97 } 98 } 99 100 printf("%.1f\n",1.0*wait_time/60.0/N); 101 }