map底层是红黑树,可用于排序
分别用enter和leave时的timeStamp作key即可
注意用rbegin取reverse_iterator获得最后一个元素
#include<iostream> #include<string> #include<map> using namespace std; struct timeStamp{ int hour; int minute; int second; bool friend operator<(timeStamp a,timeStamp b){ if(a.hour==b.hour){ if(a.minute==b.minute){ return a.second<b.second; } else { return a.minute<b.minute; } } else { return a.hour<b.hour; } } }; struct record{ string id; timeStamp enter,leave; }; int main(){ int n; string id; map<timeStamp,string> enter; map<timeStamp,string> leave; timeStamp enterStamp; timeStamp leaveStamp; while(cin>>n){ for(int i=0;i<n;i++){ cin>>id; cin>>enterStamp.hour; cin.ignore(1); cin>>enterStamp.minute; cin.ignore(1); cin>>enterStamp.second; cin.ignore(1); cin>>leaveStamp.hour; cin.ignore(1); cin>>leaveStamp.minute; cin.ignore(1); cin>>leaveStamp.second; cin.ignore(1); enter[enterStamp] = id; leave[leaveStamp] = id; } map<timeStamp,string>::iterator first = enter.begin(); map<timeStamp,string>::reverse_iterator last = leave.rbegin(); cout<<first->second<<" "<<last->second<<endl; } return 0; }