题目:胖达的山头

题目描述:

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;
}
相关推荐
Navigator_Z4 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH5 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶6 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
雪落漂泊6 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
LabVIEW开发8 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔9 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab
青少儿编程课堂10 小时前
蓝桥杯青少组图形化省考报名倒计时·全国青少年信息素养大赛·电子学会图形化考级:校园黑白棋人机对战解析
c++·python·算法·bfs·信息学竞赛
jktantan11 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构
Dream it possible!1 天前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划