活动选择问题
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
sdut 大学生艺术中心每天都有n个活动申请举办,但是为了举办更多的活动,必须要放弃一些活动,求出每天最多能举办多少活动。
Input
输入包括多组输入,每组输入第一行为申请的活动数n(n<100),从第2行到n+1行,每行两个数,是每个活动的开始时间b,结束时间e;
Output
输出每天最多能举办的活动数。
Example Input
12 15 20 15 19 8 18 10 15 4 14 6 12 5 10 2 9 3 8 0 7 3 4 1 3
Example Output
5
Hint
题解:这道题和上一道“活动选择”换汤不换药,一样的思路和方法,只是输出有所不同。
#include<bits/stdc++.h>
using namespace std;
struct time
{
int b;
int e;
}t[105];
bool cmp(struct time a, struct time b)
{
if(a.e < b.e)
return 1;
return 0;
}
int main()
{
int n,i,j;
int num;
while(~scanf("%d", &n))
{
num = 1;
for(i = 1; i <= n; i++)
{
scanf("%d%d", &t[i].b, &t[i].e);//输入开始和结束时间
}
sort(t+1,t+n+1,cmp);//按照活动的结束时间升序排列,也就是说活动结束越早在序列中越靠前
for(i = 2, j = 1; i <= n; i++)
{
if(t[i].b >= t[j].e)//当此次活动的开始时间大于或等于上个活动的结束时间,符合要求,计数
{
j = i;
num++;
}
}
printf("%d\n", num);
}
return 0;
}