L2-042 老板的作息表
解题思路
本题比较水
题目保证区间不会重叠
用map来存区间,map<string,string>,注意:map<string,string>本质上是一个pair,可以用[first,second]来表示
#include <iostream>
#include <map>
using namespace std;
int main()
{
int n;
cin >> n;
map<string, string> tasks;
for (int i = 0; i < n; i++)
{
string s1, sep, s2;
cin >> s1 >> sep >> s2;
tasks[s1] = s2; // 自动按开始时间排序
}
string last_end = "00:00:00";
for (auto const& [start, end] : tasks)
{
if (start != last_end)
{
// 如果当前任务的开始时间不等于上个任务的结束时间,说明有空隙
cout << last_end << " - " << start << endl;
}
last_end = end; // 更新当前已覆盖的最晚时间
}
// 最后检查是否漏掉了到 23:59:59 的一段
if (last_end != "23:59:59")
{
cout << last_end << " - 23:59:59" << endl;
}
return 0;
}

浙公网安备 33010602011771号