代码:
#include<iostream>
using namespace std;
#include<cstdio>
#include<algorithm>
int n,cnt,cur;
struct test
{
int start, end;
}a[1000010];
bool cmp(test x, test y)
{
return x.end < y.end;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].start >> a[i].end;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
if (a[i].start >= cur)
{
cur = a[i].end;
cnt++;
}
}
cout << cnt;
return 0;
}
总结:
有几点需要注意一下
1.为什么按照结束时间来排序,然后每次顺着找符合条件的开始时间即可就可以达到最多的参赛效果,这个原理不是特别理解,但是现在先记住吧,就是按结束时间来排序,然后根据下一个的开始时间大于上一个结束时间就可以选取了。
2.sort如果来排序结构体,可以自己定义一个bool函数然后放入sort第三个位置就可以实现想要的排序效果。