题目:胖达的山头

题目描述:

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


解题思路:

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


代码:

cpp 复制代码
#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;
}
相关推荐
Jayden_Ruan9 小时前
C++分解质因数
数据结构·c++·算法
bubiyoushang8889 小时前
MATLAB实现雷达恒虚警检测
数据结构·算法·matlab
wu_asia9 小时前
编程技巧:如何高效输出特定倍数数列
c语言·数据结构·算法
AlenTech9 小时前
207. 课程表 - 力扣(LeetCode)
算法·leetcode·职场和发展
练习时长一年10 小时前
LeetCode热题100(杨辉三角)
算法·leetcode·职场和发展
lzllzz2310 小时前
bellman_ford算法
算法
栈与堆11 小时前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
sunfove11 小时前
麦克斯韦方程组 (Maxwell‘s Equations) 的完整推导
线性代数·算法·矩阵
Rui_Freely11 小时前
Vins-Fusion之 SFM准备篇(十二)
人工智能·算法·计算机视觉
yyy(十一月限定版)11 小时前
matlab矩阵的操作
算法·matlab·矩阵