六场会最多能排下几场
六场会的起止时间是 (1,4)(2,3)(3,5)(0,7)(5,6)(6,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() {
cout << sched({{1, 4}, {2, 3}, {3, 5}, {0, 7}, {5, 6}, {6, 8}}, [](const Seg& a, const Seg& b) { return a.e < b.e; }) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)