字符串直接比较即可。
偷懒用的sort排序,O(nlogn)的复杂度;若是直接用string存最早到最晚离开的信息,只需要O(n).
代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
struct Person
{
string id;
string enter;
string leave;
};
bool enter_cmp(const Person& a, const Person& b)
{
return a.enter < b.enter;
}
bool leave_cmp(const Person& a, const Person& b)
{
return a.leave > b.leave;
}
int main()
{
int n, m;
cin >> n;
while (n --)
{
vector<Person> vt;
cin >> m;
while (m --)
{
Person p;
cin >> p.id >> p.enter >> p.leave;
vt.push_back(p);
}
sort(vt.begin(), vt.end(), enter_cmp);
cout << vt.front().id << " ";
sort(vt.begin(), vt.end(), leave_cmp);
cout << vt.front().id << endl;
}
return 0;
}