蒟蒻题解
看完这道题,立马贪心策略就浮现出来:
对每次比赛的结束时间排序。于是,我们就可以先定义一个结构体数组,来储存比赛开始的左端点与结束的右端点。然后进行排序,最后来一个判断,就完美AC。
AC代码如下
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e7 + 5;
struct node {
int Left_, Right_; // 开始时间与结束时间
bool operator < (const node &x) {
return Right_ < x.Right_;
}
}a[ MAXN ];
int n, Total_, Now_Right_;
int main () {
scanf ("%d", &n);
for (int i = 1; i <= n; i ++) {
scanf ("%d %d", &a[ i ].Left_, &a[ i ].Right_);
}
sort (a + 1, a + n + 1); //排序
for (int i = 1; i <= n; i ++) {
if (Now_Right_ <= a[ i ].Left_) { //别忘了等于
Total_ ++;
Now_Right_ = a[ i ].Right_; //更新当前右端点
}
}
printf ("%d\n", Total_);
return 0;
}