挑最短的会反而排得少

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

三场会 (0,4)(3,5)(4,8),一边按时长最短挑,一边按结束最早挑。

运行下面这段程序:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Seg {
    int s, e;
};

template <class Cmp>
int sched(vector<Seg> iv, Cmp cmp) {
    // 按 cmp 排好,从头挑,不重叠就要
    stable_sort(iv.begin(), iv.end(), cmp);
    int last_end = -1;
    int n = 0;
    for (const Seg& x : iv) {
        if (x.s >= last_end) {
            n++;
            last_end = x.e;
        }
    }
    return n;
}

int main() {
    vector<Seg> iv = {{0, 4}, {3, 5}, {4, 8}};
    cout << sched(iv, [](const Seg& a, const Seg& b) { return a.e - a.s < b.e - b.s; }) << "/" << sched(iv, [](const Seg& a, const Seg& b) { return a.e < b.e; }) << endl;
}

(本题用 g++ -std=c++17 -O0 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论
🔒 登录并作答后可看讨论