把到达时间和出发时间存到节点里面,按时间排序。有车到达A站,就left_A++。有车从A出发,如果left_A>0,则left_A--,否则cnt_A++。至于turnaround time ,则直接加到火车的到达时间上面,相当与晚点好了。 #include <iostream> using namespace std; const int maxn=105; struct node { int time; int state;//1 A start 2 A arrive 3 B start 4 B arrive }timeTable[maxn*2]; int CMP(const void* a,const void* b) { node* c=(node*)a; node* d=(node*)b; if(c->time==d->time) return d->state-c->state; else return c->time-d->time; } int Change(char str[]) { int t=(str[0]-'0')*10+(str[1]-'0'); int m=(str[3]-'0')*10+(str[4]-'0'); return t*60+m; } int T,N,NA,NB; int main() { freopen("d://B-large-practice.in","r",stdin); freopen("d://B-large-practice.out","w",stdout); scanf("%d",&N); char temp[10]; for(int i=1;i<=N;i++) { scanf("%d",&T); scanf("%d%d",&NA,&NB); for(int i=0;i<NA;i++) { scanf("/n%s",temp); timeTable[i*2].time=Change(temp); timeTable[i*2].state=1; scanf("%s",temp); timeTable[i*2+1].time=Change(temp)+T; timeTable[i*2+1].state=4; } for(int i=NA;i<NA+NB;i++) { scanf("%s",temp); timeTable[i*2].time=Change(temp); timeTable[i*2].state=3; scanf("%s",temp); timeTable[i*2+1].time=Change(temp)+T; timeTable[i*2+1].state=2; } qsort(timeTable,(NA+NB)*2,sizeof(timeTable[0]),CMP); int left_A=0,left_B=0,cnt_A=0,cnt_B=0; for(int i=0;i<(NA+NB)*2;i++) { if(timeTable[i].state==1) { if(left_A>0) left_A--; else cnt_A++; } else if(timeTable[i].state==2) { left_A++; } else if(timeTable[i].state==3) { if(left_B>0) left_B--; else cnt_B++; } else { left_B++; } } printf("Case #%d: %d %d/n",i,cnt_A,cnt_B); } return 0; }