3818. 餐厅
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 5 * 1e5 + 10;
int n, a, b, ans;
struct node {
int l, r;
node(int a, int b) { this->l = a, this->r = b; }
bool operator<(const node a) const {
if (r != a.r)
return r > a.r;
return l < a.l;
}
};
priority_queue<node> p;
void solve() {
node s = p.top();
p.pop();
int end = s.r;
ans++;
while (!p.empty()) {
s = p.top();
p.pop();
if (s.l <= end)
continue;
else {
end = s.r;
ans++;
}
}
cout << ans << endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
p.push(node(a, b));
}
solve();
return 0;
}
题解:这里考的逻辑就是,安排任务日程要先对结束时间进行排序,再对开始时间进行排序,因为结束得早可以尽可能早地开始下一个任务,如果先对开始时间排序,但开始得早不意味着结束得早,这是不划算,不贪心的。