贪心策略:
每次选择结束时间最早的活动
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n;
struct pp
{
int a, b;
} p[N];
bool cmp(pp x, pp y)
{
return x.b < y.b;
}
int ans = 0;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> p[i].a >> p[i].b;
}
sort(p + 1, p + n + 1, cmp);
int temp = 0;
for (int i = 1; i <= n; i++)
{
if (p[i].a >= temp)
{
temp= p[i].b;
ans++;
}
}
cout << ans;
}