贪心问题
#include<bits/stdc++.h>
#define lc ((o) << 1)
#define rc ((o) << 1 | 1)
using namespace std;
typedef long long ll;
typedef pair<int,int>Pair;
const int N=1e5+10;
const int MOD=1e9+7;
struct note
{
int x,y,z;
}e[N];
bool cmp(note a,note b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x<b.x;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>e[i].x>>e[i].y;
}
sort(e,e+m,cmp);
int temp=0;
if(e[0].x>1)
{
printf("-1\n");
return 0;
}
int l=1,r=1,ans=1;//辅助状态,衔接
for(int i=0;i<m;i++)
{
if(e[i].x<=l)//与上一个状态的l比较,谁的区间右值更大
{
r=max(r,e[i].y);
}
else
{
ans++;
l=r+1;
if(e[i].x<=l)
{
r=max(r,e[i].y);
}
else
{
printf("-1\n");
return 0;
}
}
if(r>=n)
break;
}
printf("%d\n",r>=n?ans:-1);
}