PAT (Advanced Level) Practice 1006 Sign In and Sign Out

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

题目大意

有 M 个人,第一个进入机房的人开门,最后一个离开机房的人关门。已知 M 个人的ID、进入机房的时间、离开机房的时间。要找出开门人的编号和关门人的编号。

题目分析

时间的大小关系和字符串大小关系是相同的。
按照进入时间从小到大排序后,第一个人就是开门人。
按照离开时间从大到小排序后,第一个人就是关门人。
题目中没有给出具体的 M 的范围,所以使用vector进行存储,当然也可以开一个较大的数组。

AC代码

#include <bits/stdc++.h>
using namespace std;
struct node
{
    string id, in, out;
} p;
vector<node> v;
bool cmp1(const node &s1, const node &s2)
{
    return s1.in < s2.in;
}
bool cmp2(const node &s1, const node &s2)
{
    return s1.out > s2.out;
}
int main()
{
    int m, i;
    cin >> m;
    while (m--)
    {
        cin >> p.id >> p.in >> p.out;
        v.push_back(p);
    }
    sort(v.begin(), v.end(), cmp1);
    cout << v[0].id << " ";
    sort(v.begin(), v.end(), cmp2);
    cout << v[0].id << endl;
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部