线段覆盖
线段覆盖
那么长时间了,终于敢于做完这道题了
我说是我懒你信不?
- 贪心
- 右端点越往左,对后面的"限制"越小
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=1e6+1;
struct seq{
int a,b;
bool operator < (const seq& x)const{
if(b!=x.b)return b<x.b;
return a<x.a;
}
}s[MAXN];
inline int read()
{
int f=1,x=0;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return f*x;
}
int main()
{
int n=read();
for(int i=1;i<=n;i++)
{
s[i].a=read();s[i].b=read();
}
sort(s+1,s+n+1);
int lastE=s[1].b,cnt=1;
for(int i=2;i<=n;i++)
{
if(s[i].a>=lastE){
cnt++;
lastE=s[i].b;
}
}
printf("%d",cnt);
return 0;
}