贪心之活动选择问题

来源:互联网 发布:python 交易策略 编辑:程序博客网 时间:2024/05/16 05:19

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>
#include <stdlib.h>
#include <string.h>
struct mem
{
    int begin, end;
} s[110], t;
int main()
{
    int n, i, j;
    while(scanf("%d", &n) != EOF)
    {
        for(i = 0; i < n; i++)
        {
            scanf("%d%d", &s[i].begin, &s[i].end);
        }
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < n-1-i; j++)
            {
                if(s[j].end > s[j+1].end)
                {
                    t = s[j+1]; s[j+1] = s[j]; s[j] = t;
                }
            }
        }
        int time = 0, k = 0;
        for(i = 0; i < n; i++)
        {
            if(s[i].begin >= time)
            {
                time = s[i].end;
                k++;
            }
        }
        printf("%d\n", k);
    }
    return 0;
}

0 0