其他分享
首页 > 其他分享> > AcWing 905. 区间选点 贪心

AcWing 905. 区间选点 贪心

作者:互联网


参考

排序要排右端点。

#include<bits/stdc++.h>
using namespace std;
#define fir(i,a,n) for(int i=a;i<=n;i++)
//======================
const int N=1e5+10;
struct node
{
	int a,b;
}a[N];
int n;
bool cmp(node a,node b)
{
	return a.b<b.b;
}
int main()
{
	cin>>n;
	fir(i,1,n) 
	{
		int aa,bb;cin>>aa>>bb;
		a[i]={aa,bb};
	}
	sort(a+1,a+1+n,cmp);
	int ans=1;
	int now=a[1].b;
	for(int i=2;i<=n;i++)
	{
		//cout<<now<<" "<<a[i].b<<endl;
		if(now<a[i].a)
		{
			now=a[i].b;ans++;
		}
	}
	cout<<ans;
	return 0; 
}

标签:选点,aa,cout,bb,905,int,fir,ans,AcWing
来源: https://blog.csdn.net/karshey/article/details/121254336