题目:胖达的山头

题目描述:

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;
}
相关推荐
liulilittle5 小时前
认识麻将 —— 流水及对账
算法·游戏·lua·对账·流水·资金·玩法
郝学胜-神的一滴9 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
青少儿编程课堂1 天前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
geovindu1 天前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗1 天前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
凌冰_1 天前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗1 天前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗1 天前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
lisw052 天前
稀疏算法概述!
算法·机器人·无人机