题目链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928
题目分析:
根据一天中多人到达机房时间和离开时间,给出开门以及锁门人的ID。显然可以对到达时间,以及离开时间进行排序,最早的时间即为第一个人到达时间,最晚时间即为最后一个人离开时间。但是这容易超时。转换思路,可以想到因为只需知道最早到达时间和最晚离开时间,那么在输入的时候就可以对其进行筛选。
参考代码:
#include <iostream>
#include <cstdio>
using namespace std;
struct Pnode{
char id[20];
int h, m, s;
}ans1, ans2, temp;
bool great(Pnode node1, Pnode node2){
return a.h*3600 + a.m*60 + a.s > b.h*3600 + b.m*60 + b.s;
}
int main(){
int n;
scanf( "%d", &n) ;
ans1.h = 0, ans1.m = 0, ans1.s = 0; //将最迟的时间令为ans1,初始设置值为0点
ans2.h = 24, ans2.m = 60, ans2.s = 60; //将最早的时间令为ans2,初始设置值为24点
for(int i = 0; i < n; i++){
scanf( "%s %d:%d:%d", temp.id, &temp.h, &temp.m, &temp.s);
if(great(ans2, temp)) ans2=temp;
scanf(" %d:%d:%d",&temp.h, &temp.m, &temp.s);
if(great(temp, ans1)) ans1=temp;
}
printf("%s %s", ans2.id ,ans1.id);
return 0;
}