贪心B 活动选择问题(很经典)

来源:互联网 发布:mac删除系统文件 编辑:程序博客网 时间:2024/06/05 16:24

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

#include <stdio.h>struct hd{    int b;    int e;}data[110],c;int main(){    int n,i,j;    while(~scanf("%d",&n))    {        for(i=0;i<n;i++)        {            scanf("%d %d",&data[i].b,&data[i].e);        }        for(i=0;i<n-1;i++) //将结束的时间按照升序的序列排序,冒泡排序        {            for(j=0;j<n-i-1;j++)            {                if(data[j].e>=data[j+1].e)                {                    c=data[j];                    data[j]=data[j+1];                    data[j+1]=c;        //交换的时候两个时间都交换,用结构体!                }            }        }        int timestart=0;  //储存承接变量:上一个的结束时间        int num=0;     //举办的活动个数        for(i=0;i<n;i++)        {            if(data[i].b>=timestart)            {                num+=1;                timestart=data[i].e;            }        }        printf("%d\n",num);    }    return 0;}