PAT Advanced 1006. Sign In and Sign Out

发布时间 2023-05-04 11:48:51作者: 十豆加日月

PAT Advanced 1006. Sign In and Sign Out

1. Problem Description:

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.

2. 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.

3. 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.

4. Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

5. Sample Output:

SC3021234 CS301133

6. Performance Limit:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

题目要求找出最早签到和最晚签退的人,定义map<int, string>类型变量signInsignOut分别记录每个人的签到和签退,其中key存储对小时、分钟和秒加权得到的正整数,value存储每个人的ID。利用map默认按照key升序排列的规则进行输出,这里注意end()返回的是尾后迭代器。

参考大佬题解:1006. Sign In and Sign Out (25)-PAT甲级真题_柳婼的博客-CSDN博客 ,维护最早和最晚时间即相应ID即可,没必要把所有记录都存下来。

My Code & Result:

#include <iostream>
#include <map>
#include <string>

#define HOUR_WEIGHT 10000
#define MIN_WEIGHT 100

using namespace std;

int main(void)
{
    int recordNum=0;
    string tempId;
    int hour=0, min=0, sec=0;
    int i=0; // iterator
    int weight=0;
    map<int, string> signIn;
    map<int, string> signOut;
    
    cin >> recordNum;

    for(i=0; i<recordNum; ++i)
    {
        cin >> tempId;
        scanf("%d:%d:%d", &hour, &min, &sec);
        weight = hour*HOUR_WEIGHT + min*MIN_WEIGHT + sec;
        signIn[weight] = tempId;

        scanf("%d:%d:%d", &hour, &min, &sec);
        weight = hour*HOUR_WEIGHT + min*MIN_WEIGHT + sec;
        signOut[weight] = tempId;
        
        // cin >> tempId >> hour >> min >> sec;
        // cout << tempId << " " << hour << " " << min << " " << sec << endl;
    }

    cout << signIn.begin()->second << " " << (--signOut.end())->second << endl;
    // cout << signIn.begin()->second << " " << signOut.end()->second << endl; // caution: end() return iterator point to one past the last
    
    return 0;
}
Compiler
C++ (g++)
Memory
456 / 65536 KB
Time
4 / 400 ms