2073活动选择问题

来源:互联网 发布:牧在线网络汉语汉字典 编辑:程序博客网 时间:2024/06/14 17:04

活动选择问题

Time Limit: 1000MS Memory Limit: 65536KB
SubmitStatistic Discuss

Problem Description

 sdut 大学生艺术中心每天都有n个活动申请举办,但是为了举办更多的活动,必须要放弃一些活动,求出每天最多能举办多少活动。

Input

 输入包括多组输入,每组输入第一行为申请的活动数n(n<100),从第2行到n+1行,每行两个数,是每个活动的开始时间b,结束时间e;

Output

 输出每天最多能举办的活动数。

Example Input

1215 2015 198 1810 154 146 125 102 93 80 73 41 3

Example Output

5

思路:将活动结束时间依次由小到大排列,如果结束时间相同,则按起始时间由小到大排列,然后依次选择时间不交叉的时间段作为选中的活动。

#include<stdio.h>struct activity{    int s;    int e;};void sort(struct activity *a,int n){    int i,j,temp1,temp2;    for(i=0;i<n-1;i++)    {        for(j=i+1;j<n;j++)        {            if(a[i].e>a[j].e)            {                temp1=a[i].e;                a[i].e=a[j].e;                a[j].e=temp1;                temp2=a[i].s;                a[i].s=a[j].s;                a[j].s=temp2;            }        }    }    //for(i=0;i<n;i++)    //    printf("%d %d) ",a[i].s,a[i].e);    //printf("**\n");    for(i=0;i<n-1;i++)    {        for(j=i+1;j<n;j++)        {            if(a[i].e==a[j].e&&a[i].s>a[j].s)            {                temp1=a[i].s;                a[i].s=a[j].s;                a[j].s=temp1;                temp2=a[i].e;                a[i].e=a[j].e;                a[j].e=temp2;            }        }    }}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        struct activity a[n];        int i;        for(i=0;i<n;i++)            scanf("%d %d",&a[i].s,&a[i].e);        sort(a,n);        //for(i=0;i<n;i++)        //printf("%d %d) ",a[i].s,a[i].e);        //printf("\n");        int count=1,time=a[0].e;        for(i=1;i<n;i++)        {            if(a[i].s>=time)            {                time=a[i].e;                count++;            }        }        printf("%d\n",count);    }    return 0;}


原创粉丝点击