区间贪心-

0x00 题目地址

Problem - 2037 (hdu.edu.cn)

0x01 分析

要尽可能的多看节目,那么就要每一步都选择结束时间最短的,这样就可以留下尽可能多的时间去看别的节目。

因此也就是将各个节目的结束时间从早到晚排好序。

然后依次判断节目是否可以观看:

①当前节目是否已经开始,如果开始了,就接着寻找下一个还没开始的节目。

然后更新当前的时间为找到的节目的截止时间。

0x02 完整代码

复制代码
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
struct programe {
    int id;
    int startTime;
    int endTime;
    programe(int id, int startTime, int endTime) :id(id), startTime(startTime),endTime(endTime){}
};

bool compare_pro(programe A, programe B) {
    return A.endTime <  B.endTime;
}


// 测试函数
int main() {
    int n = 0;
    while (cin >> n) {
        vector<programe> case_vector(n);
        int startTime = 0;
        int endTime = 0;
        for (int i = 0; i < n; i++) {
            cin >> startTime >> endTime;
            programe temp(i, startTime, endTime);
            case_vector.push_back(temp);
        }

        sort(case_vector.begin(),case_vector.end(),compare_pro);

        int currentTime = 0;
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (case_vector[i].startTime >= currentTime) {
                count++;
                currentTime = case_vector[i].endTime;
            }

        }

        cout << count << endl;

    }
    return 0;
}
相关推荐
政企项目老覃9 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水10 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR10 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵10 小时前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽10 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Nil20811 小时前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
203号居民12 小时前
LeetCode hot 100 — 25. K 个一组翻转链表
算法·leetcode·链表
ocean210313 小时前
2025-2026年AI算法与模型研发面试高频知识点洞察
人工智能·算法·面试
Nil20813 小时前
leetcode 78子集
数据结构·算法·leetcode