贪心(入门简单题)

有若干个活动,第i个开始时间和结束时间是[Si,fi),只有一个教室,活动之间不能交叠,求最多安排多少个活动?

Input
第一行一个正整数n (n <= 10000)代表活动的个数。
第二行到第(n + 1)行包含n个开始时间和结束时间。
开始时间严格小于结束时间,并且时间都是非负整数,小于1000000000
Output
一行包含一个整数表示活动个数。
Input示例
3
1 2
3 4
2 9
Output示例
2
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+5;
struct node
{
	int s,e;
} a[maxn];
bool  cmp(node x,node y)
{
	if(x.e<y.e) return true;
	else if(x.e==y.e&&x.s>y.s) return true;
	return false;
}
int main()
{
	int n,i,j,ans,end;
	cin>>n;
	for(i = 0;i<n;i++)
		cin>>a[i].s>>a[i].e;
	sort(a,a+n,cmp);
	ans = 0;
	end = -1e9-100;
	for(i =0;i<n;i++)
	{
		if(a[i].s>=end)
		{
			ans++;
			end=a[i].e;
		}
	}
	cout<<ans<<endl;
	return 0;
}
/*
3
1 2
3 4
2 9
*/ 


猜你喜欢

转载自blog.csdn.net/qq_41156122/article/details/80548066