题目:胖达的山头

题目描述:

PTA | 程序设计类实验辅助教学平台


解题思路:

        根据题意是多个区间,那么我们可以想到使用差分,前缀和解决。


代码:

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    vector<pair<int, int>> v;  
    while (n--) {
        int a, b, c;
        int d, e, f;
        scanf("%d:%d:%d", &a, &b, &c);  // scanf拆分字符串得对应变量。改为 %d 因为变量是 int
        scanf("%d:%d:%d", &d, &e, &f);
        int l = a * 3600 + b * 60 + c;
        int r = d * 3600 + e * 60 + f;
        v.push_back({l, r});
    }
    sort(v.begin(), v.end());
    int num = 24 * 3600;
    vector<int> all(num + 2, 0);  // 稍微扩大一点防止越界
    for (auto p : v) {  // c++11遍历pair数组是这样的******
        all[p.first]++;
        all[p.second + 1]--;
    }
    int cnt = 0;  // 添加 cnt 的声明
    int mx = 0;
    for (int i = 0; i <= num; i++) {
        cnt += all[i];
        mx = max(mx, cnt);
    }
    cout << mx << "\n";
}

int main() {
    solve();  // 修正拼写错误
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值