Description
Input
Output
Sample Input
2 4
1 12
4 5
7 10
7 8
Sample Output
HINT
第1,3,4共3只奶牛可以同時喫草,第1,3,5也可以.
要去取儘量多的區間,使得區間兩兩不重
按照右端點排序然後貪心。因爲右端點小顯然接下來可以取更多的區間
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100010
#define pa pair<int,int>
int n,now,ans;
pa a[N];
inline bool cmp(const pa &a,const pa &b)
{return a.second<b.second;}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d%d",&a[i].first,&a[i].second);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
if (now<=a[i].first)
{
now=a[i].second;
ans++;
}
}
printf("%d\n",ans);
}