HDU-2037(c++解法)

今年暑假不AC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 75705 Accepted Submission(s): 40689

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

Author

lcy

Source

ACM程序设计期末考试(2006/06/07)

Recommend

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


今天有学长跟我说,这题是去年选拔赛的压轴题,我就来做做,正好刚会结构体排序没多久,看到这题一下子就想到了结构体排序+贪心
这题核心思想就是贪心,用sort函数进行结构体排序(可怜我排序算法都快忘没了=.=)
三目运算符(条件运算符)用得我很爽啊有木有!!!
用起来高大上还好看啊有木有!!!

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct ac
{
    int start;
    int endd;
};
bool cmp(ac a, ac b);
int main()
{
    struct ac time[102];
    int test_num, i, j, flag;
    while(scanf("%d", &test_num) != EOF && test_num != 0)
    {
        flag = 1;
        for(i = 0; i < test_num; i++)
            scanf("%d%d", &time[i].start, &time[i].endd);
        sort(time, time + test_num, cmp);
        for(i = 0, j = i + 1; j < test_num;)
        {
            if(time[i].endd <= time[j].start)
            {
                flag++;
                i = j;
                j++;
            }
            else
                j++;
        }
        printf("%d\n", flag);
    }
    return 0;
}

bool cmp(ac a, ac b)
{
    return a.endd == b.endd ? a.start < b.start : a.endd < b.endd;
}

2018.10.04 21:43 写于我老姨家中

猜你喜欢

转载自blog.csdn.net/qq_43005180/article/details/82940755
今日推荐