解題思路:
區間貪心,貪心策略爲每次選擇結束時間最早的區間,同時使用了優先隊列來按照結束時間的前後保存各個區間。
AC代碼:
#include<iostream>
#include<stdio.h>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
struct node {
int l, r;
bool operator < (const node &a) const {
return r > a.r;//結束時間早的區間優先級高
}
}list[101];
priority_queue<node> q;
int main() {
int n;
while (cin >> n) {
if (n == 0)break;
//while (!q.empty())q.pop();
for (int i = 1; i <= n; i++) {
cin >> list[i].l >> list[i].r;
q.push(list[i]);
}
int ans = 0; int now = 0;
while (!q.empty()) {
node tmp = q.top(); q.pop();
if (tmp.l < now) { continue; }
ans++;
now = tmp.r;
}
cout << ans << endl;
}
return 0;
}