题目详情 - L2-042 老板的作息表 (pintia.cn)
这题考察区间操作+格式输入+结构体排序,将时间化成秒来计算会比较方便
#include<iostream> //ac
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
int a,b;
bool operator<(const Node &t) const {
if(a != t.a) return a < t.a;
return b < t.b;
}
};
vector<Node> sq;
int gettime(int h,int m,int s){
return h*60*60+m*60+s;
}
void print(int a,int b){
int h1 = a/3600,m1 = a/60%60,s1 = a%60;
int h2 = b/3600,m2 = b/60%60,s2 = b%60;
printf("%02d:%02d:%02d - %02d:%02d:%02d\n",h1,m1,s1,h2,m2,s2);
}
int main(){
int n; cin>>n;
for(int i = 0; i < n; i++){
int h1,m1,s1,h2,m2,s2;
scanf("%d:%d:%d - %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
sq.push_back({gettime(h1,m1,s1), gettime(h2,m2,s2)});
}
sort(sq.begin(),sq.end());
int t = 0;
for(int i = 0; i < n; i++){
if(t < sq[i].a){
print(t,sq[i].a);
}
t = sq[i].b;
}
int maxn = gettime(23,59,59);
if(t != maxn){
print(t,maxn);
}
return 0;
}