L2-042 老板的作息表

题目 L2-042 老板的作息表

image-87fec611

思路分析

区间合并

0ce2b231e971504edf788801c9fc1fdf-5c7a2ca7

代码实现

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using PII = pair<int,int>;
using Pll = pair<ll,ll>;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
const int inf = 0x3f3f3f3f;

vector<PII> times;
int cnt;

void print_time(int t) {
    int h = t / 3600;
    t %= 3600;
    int m = t / 60;
    int s = t % 60;
    printf("%02d:%02d:%02d", h, m, s);
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    int n;scanf("%d",&n);
    while(n--){
        int h1,m1,s1,h2,m2,s2;
        scanf("%d:%d:%d - %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
        int start = h1 * 3600 + m1 * 60 + s1;
        int end = h2 * 3600 + m2 * 60 + s2;
        times.push_back({start, end});
    }

    sort(times.begin(), times.end());
    vector<PII> gaps;

    if (times[0].first > 0) {
        gaps.push_back({0, times[0].first});
    }
    int prev_end = times[0].second;
    for (int i = 1; i < times.size(); ++i) {
        int curr_start = times[i].first;
        if (curr_start > prev_end) {
            gaps.push_back({prev_end, curr_start});
        }
        prev_end = max(prev_end, times[i].second);
    }

    if (prev_end < 86399) {
        gaps.push_back({prev_end, 86399});
    }

    for (const auto& gap : gaps) {
        print_time(gap.first);
        printf(" - ");
        print_time(gap.second);
        printf("\n");
    }

    return 0;
}

同类题型

视频讲解


⬅️ L2-041 插松枝 🏠 00-天梯赛 ➡️ L2-043 龙龙送外卖