PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

题目描述:

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.

译:每个输入文件包含一个测试用例,每个测试用例包含一天的记录。这个用例以一个正整数 M 开始,表示总的记录条数。接下来 M 行,格式如下:

ID_number Sign_in_time Sign_out_time

给出的时间格式为:HH:MM:SS , 并且 ID_number 是一个不超过 15 个字符的字符串。


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.

译:对于每个测试用例,在一行中输出这一天中开门的人和锁门的人的 ID number 。这两个 ID number 之间必须用一个空格分开。

注意:题目保证记录是符合实际的,意味着每个人的签到的时间必须比签退的时间更早,并且没有两个人同时签到或者签退。


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

The Idea:

第一种方法: 很明显的是使用结构体的排序。首先就想到了 sort 。对于sort的排序是很熟悉了,所以一次就 AC 了 。

第二种方法:由于题目中并没有指出 M 的具体范围(写者首先在第一种方法中内置 MAX1000000,结果爆内存了),所以就想着使用动态添加记录的。而需要排序,故选择了 map (map 既能按照 key 升序降序排列也能按照 value 升序降序排,为了简单且题目中说明 签到时间和签退时间不会重复,所以可以利用时间作为 key 值,ID_number 作为 value 值,这样就可以分别拿到 签到时间升序 和签退时间降序的 第一个keyvalue 值就是本题答案。)


The Codes —— sort:

#include<bits/stdc++.h>
using namespace std ;
#define MAX 10010
typedef struct node{
string name ;
string inTime ;
string outTime ;
}Node ;
Node peo[MAX] ;
int m ;
bool cmp1(Node a , Node b){
return a.inTime < b.inTime ; // 直接比较字符串的大小 , cmp1 表示按照签到时间先后排序
}
bool cmp2(Node a ,Node b){
return a.outTime > b.outTime ; // cmp2 表示按照签退时间倒序排序
}
int main(){
cin >> m ;
getchar() ;
for(int i = 0 ; i < m ; i ++){
cin >> peo[i].name >> peo[i].inTime >> peo[i].outTime ;
}
sort(peo , peo + m , cmp1);
string s1 = peo[0].name ;
sort(peo , peo + m , cmp2) ;
s1 += " " + peo[0].name ;
cout << s1 << endl ;
return 0 ;
}

The Codes —— map:

#include<bits/stdc++.h>
using namespace std;
map<string,string,less<string> > in ; // 按照 关键字升序排列的 map
map<string,string,greater<string> > out ; // 按照 关键字降序排列的 map
int m ;
string name , inTime , outTime , ans ;
int main(){
cin >> m ;
getchar() ; // 吸收输入 m 之后的 换行符
for(int i = 0 ; i < m ; i ++){
cin >> name >> inTime >> outTime ;
in[inTime] = name ; // 建立 签到时间 到 ID_number 的 map
out[outTime] = name ; // 建立 签退时间 到 人名 的 map
}
map<string , string ,less<string> >::iterator it = in.begin() ;// 关键字升序迭代器
map<string , string , greater<string> >::iterator it2 = out.begin() ;// 关键字降序迭代器
ans = it -> second ; // 最早签到的 ID_number
ans += " " + it2 -> second ; // 最晚签退的 ID_number
cout << ans << endl ;
return 0 ;
}

PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642的更多相关文章

  1. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  2. PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...

  3. PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...

  4. PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...

  5. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  6. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  7. PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) (排序)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  8. PAT (Advanced Level) Practice 1002 A+B for Polynomials 分数 25

    This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...

  9. PAT (Basic Level) Practice (中文)1030 完美数列 (25 分) (有点意思)

    给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...

随机推荐

  1. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  2. how to config custom process.env in node.js

    how to config custom process.env in node.js process.env APP_ENV NODE_ENV https://nodejs.org/api/proc ...

  3. 智能货柜 & 技术原理 (动态视觉识别 + 重力感应)

    智能货柜 & 技术原理 (动态视觉识别 + 重力感应) 智能货柜 拥有智能化.精细化运营模式的智能货柜成为代替无人货架继前进的方式. 相比无人货架来说,智能货柜的技术门槛更高,拥有 RFID. ...

  4. linux bash which

    linux bash which https://linuxize.com/post/linux-which-command/ Linux which command is used to ident ...

  5. Flutter使用WebSockets

    文档 注意是WebSockets而不是socket.io install dependencies: web_socket_channel: demo import 'dart:convert'; i ...

  6. 「NGK每日快讯」11.24日NGK公链第22期官方快讯!

  7. 02.Fancy Indexing

    import numpy as np x = np.arange(16) index = [3,5,8] x[index] array([3, 5, 8]) X = x.reshape(4,-1) X ...

  8. Java基础语法:基本数据类型

    Java是一种强类型语言,每个变量都必须声明其类型. Java的数据类型 分为两大类:基本类型(primitive type)和引用类型(reference type). Java的所有八种基本类型的 ...

  9. 零基础学Python:数据容器

    1.常用操作 列表常用操作 在 ipython 中定义一个 列表,例如: l= list() 输入 l. 按下 TAB 键, ipython 会提示 字典 能够使用的函数如下: 可以到官方网址查询使用 ...

  10. 基于keras实现的中文实体识别

    1.简介 NER(Named Entity Recognition,命名实体识别)又称作专名识别,是自然语言处理中常见的一项任务,使用的范围非常广.命名实体通常指的是文本中具有特别意义或者指代性非常强 ...