原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1133
思路:线扫,加贪心,将线段按终点升序排序
AC代码;
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 50005;
struct line {
int start, end;
}lines[10005];
bool cmp(line &l1, line &l2) {
if (l1.end < l2.end)return true;
else if (l1.end == l2.end &&l1.start < l2.start)return true;
return false;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d", &lines[i].start, &lines[i].end);
}
sort(lines, lines + n, cmp);
int ans = 1, pre = lines[0].end;
for (int i = 1; i < n; i++) {
if (lines[i].start >= pre) {
ans++;
pre = lines[i].end;
}
}
printf("%d\n", ans);
return 0;
}