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. 微信小程序-云开发-实战项目

    微信小程序-云开发-实战项目 微信小程序 微信小程序平台服务条款 https://developers.weixin.qq.com/miniprogram/product/service.html h ...

  2. webpack defineConstants

    webpack defineConstants PAGES 全局常量/全局变量 https://webpack.js.org/plugins/define-plugin/ taro https://n ...

  3. SVG & gradient & color

    SVG & gradient & color https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Gradients & ...

  4. 以太坊手续费上涨,矿工出逃,VAST前景向好!

    根据最新数据显示,以太坊的Gas费用在最近几天大幅飙涨,尤其是在过去2小时内,增幅约20%,一度达到了17.67美元.而这也导致了,许多基于以太坊协议的相关项目无法被生态建设者使用,很多矿工也纷纷出逃 ...

  5. HGAME apache

    HGAME apache Linux下六十四位可执行文件 IDA找主函数 输入长度为35的字符串,经过一次函数处理,之后if条件函数的返回值为1,就能判定输入就是flag 函数sub_1447的参数 ...

  6. 行业动态 | Apache Pulsar 对现代数据堆栈至关重要的四个原因

    与 Kafka 相比,Pulsar 的架构使它在跨地域复制.扩展.多租户和队列等方面具有重要的优势.   1 月 27 日,DataStax 宣布收购Kesque(Pulsar 即服务),加入到了 P ...

  7. 使用Mongodb设计评论系统

    1:如何设计数据存储结构 1.1:mysql 1:评论表 2:回复表(评论的评论) 1.2:mongodb 不需要两张表,一个collection 就可以搞定. 数据结构如图: 通过对象数组中的字段作 ...

  8. 看完了进程同步与互斥机制,我终于彻底理解了 PV 操作

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 CS-Wiki(Gitee 官 ...

  9. Java基础语法:标识符

    Java所有的组成部分都需要名字. 类名.变量名 以及方法名 都被称为标识符. 一.规则 Ⅰ.首字符 规则:所有的标识符都应该以字母(A-Z 或者 a-z).美元符($).下划线(_)开始. 示例:t ...

  10. JUC-ThreadLocal

    目录 ThreadLocal ThreadLocal测试 ThreadLocal类结构 前言 多线程访问同一个共享变量的时候也别容易出现并发问题,特别是在多线程需要对一个共享变量进行写入的时候.为了保 ...