1.先找出贪心策略2.代码实现需要多重考虑和试数
#include <bits/stdc++.h>
using namespace std;
int cnt;
int ans;
struct record
{
int s;
int e;
}a[1000];
bool cmp(const record& x, const record& y)//结构体排序!!!注意写法
{
return x.s < y.s;
}
int main()
{
int n,m;
int cnt = 0;
int L, R;
int maxn[1000] = { 0 }, copy[1000] = { 0 };
L = 0;
cin >> n>>m;
int t;
for (int i = 1; i <= m; i++)
{
cin >> a[i].s >> a[i].e;
if (a[i].s == 0)
{
maxn[i] = a[i].e;
cnt++;
}
}
int c;
memcpy(copy + 1, maxn + 1, 1000);//****复制函数**,先写被复制函数的首位地址,然后写入复制位数。
sort(maxn + 1, maxn + 1 + m);
for (int i = 1; i <= m; i++)
if (copy[i] == maxn[m])
c = i;
R = a[c].e;
sort(a + 1, a + 1 + m, cmp);
a[1].s = 0, a[1].e = R;
for (int i = 1; i <= m; i++)
{
if (a[i].s <= R )
{
R = a[i].e;
ans++;
}
if (R == n)
break;
}
//注意特殊情况下的题目答案会有所区别,所以要多试
if (R != n)
cout << ans - 1;
else
cout << ans;
return 0;
}