At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.
Input Specification:
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS
, and ID_number
is a string with no more than 15 characters.
Output Specification:
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.
Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133
Solve
给一个ID,签入时间和签出时间,让找出最早签入时间和最晚签出时间分别是哪两个ID。
故需要自定义个时间比较函数 cmpTime
,当前者比后者晚时返回 true。
之后就维护一个最早签入时间和最晚签出时间以及其对应的ID即可,在遇到比当前记录更早签入或更晚签出的ID时就更新。
#include<iostream>
using namespace std;
bool comTime(string a, string b) {
// return true if a is later than b
int h[2], m[2], s[2];
h[0] = (a[0] - '0') * 10 + (a[1] - '0');
h[1] = (b[0] - '0') * 10 + (b[1] - '0');
m[0] = (a[3] - '0') * 10 + (a[4] - '0');
m[1] = (b[3] - '0') * 10 + (b[4] - '0');
s[0] = (a[6] - '0') * 10 + (a[7] - '0');
s[1] = (b[6] - '0') * 10 + (b[7] - '0');
if(h[0] > h[1]) return true;
if(h[0] == h[1] && m[0] > m[1]) return true;
if(h[0] == h[1] && m[0] == m[1] && s[0] > s[1]) return true;
return false;
}
int main() {
int n;
string resFirst, resLast, timeFirst = "-1", timeLast = "-1";
cin >> n;
while(n--) {
string s[3];
cin >> s[0] >> s[1] >> s[2];
if(timeFirst == "-1" && timeLast == "-1") {
resFirst = s[0], resLast = s[0], timeFirst = s[1], timeLast = s[2];
continue;
}
if(comTime(timeFirst, s[1]))
resFirst = s[0], timeFirst = s[1];
if(!comTime(timeLast, s[2]))
resLast = s[0], timeLast = s[2];
}
cout << resFirst << " " << resLast << endl;
return 0;
}
除了自己手搓一个比较函数,还可以直接使用 string 的字典序比较,因为时间格式都是一样的,而时间格式本身的字典序结果就和其比较结果一致。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Record {
string id, in_time, out_time;
};
int main() {
int n;
cin >> n;
vector<Record> records(n);
for (auto& rec : records) {
cin >> rec.id >> rec.in_time >> rec.out_time;
}
// 找最早签到的人
auto earliest = min_element(records.begin(), records.end(),
[](const Record& a, const Record& b) { return a.in_time < b.in_time; });
// 找最晚签出的人
auto latest = max_element(records.begin(), records.end(),
[](const Record& a, const Record& b) { return a.out_time < b.out_time; });
cout << earliest->id << " " << latest->id << endl;
return 0;
}