<hdu-OJ>|| 2037_今年暑假不AC || 递归

今年暑假不AC

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 103138    
Accepted Submission(s): 55094

Problem Description

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,
你一定还会看一些其它的节目,
比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,
假设你已经知道了所有你喜欢看的电视节目的转播时间表,
你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,
每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,
然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),
分别表示第i个节目的开始和结束时间,

为了简化问题,每个时间都用一个正整数表示。
n=0表示输入结束,不做处理。

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output

5

Recommand

We have carefully selected several similar problems for you:  
1050 1009 1051 1052 1789 


Problem - 2037

Submit

#include <stdio.h>
#include <stdlib.h>

typedef struct _test
{
    int left;
    int right;
} test;

void _sort(test *now);
int fun(const test *befor, const test *now, int count);


int num;

int main()
{
    while (scanf("%d", &num))
    {
        if (num == 0)
            exit(EXIT_SUCCESS);

        test *_list = (test *) malloc(sizeof(test) * num);
        for (int i = 0; i < num; i++)
            scanf("%d %d", &(_list[i].left), &(_list[i].right));

        _sort(_list);
        // for (int i = 0; i < num; i++)
        //     printf("%d %d || ", _list[i].left, _list[i].right);
        // printf("\n");
        printf("%d\n", fun(&((test){0, 0}), _list, 0));

        free(_list);
    }
}

void _sort(test *now)
{
    for (int i = 1; i < num; i++) {
        for (int j = 0; j < num - i; j++) {
            if (now[j].left > now[j + 1].left)
            {
                test temp = now[j];
                now[j] = now[j + 1];
                now[j + 1] = temp;
            }
        }
    }
}

int fun(const test *befor, const test *now, int count)
{
    if (num == count)
        return 0;

    int sum = 0, suma = 0, sumb = 0;
    if (befor->left <= now[count].left && now[count].left < befor->right) {    // 当前时段节目与之前的想冲突,无法同时观看,所以放弃
        sum = fun(befor, now, count + 1);
    } else {
        // 1 观看当前时段的节目
        suma = 1 + fun(&now[count], now, count + 1);
        // 2 不观看当前时段的节目
        sumb = 0 + fun(befor, now, count + 1);

        (suma > sumb) ? (sum = suma) : (sum = sumb);
    }

    return sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值