问题 A: 看电视

问题 A: 看电视
时间限制: 1 Sec 内存限制: 32 MB
献花: 142 解决: 86
[献花][花圈][TK题库]
题目描述
暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。
现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?
输入
输入包含多组测试数据。每组输入的第一行是一个整数n(n<=100),表示小明喜欢的节目的总数。
接下来n行,每行输入两个整数si和ei(1<=i<=n),表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。
当n=0时,输入结束。
输出
对于每组输入,输出能完整看到的电视节目的个数。
样例输入
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
样例输出
5

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef struct
{
    int x;
    int y;
}Interval;

bool cmp(Interval I1, Interval I2)
{
    if (I1.x != I2.x) return I1.x > I2.x;
    return I1.y < I2.y;
}

int main()
{
#ifdef _DEBUG
    //freopen("data.txt", "r+", stdin);
    fstream cin("data.txt");
#endif // _DEBUG
    const int MaxN = 100;
    int N;
    Interval Itv[MaxN];
    while (cin >> N, N)
    {
        for (int i = 0; i < N; ++i)
            cin >> Itv[i].x >> Itv[i].y;
        sort(Itv, Itv + N,cmp); //按左端点从大到小排序,如果左端点相同,则按照右端点从小到大排序

        int count = 1, tmpX = Itv[0].x;
        for (int i = 1; i < N; ++i)
        {
            if(Itv[i].y <= tmpX)
            {
                ++count;
                tmpX = Itv[i].x;
            }
        }
        cout << count << endl;
    }

#ifdef _DEBUG
    cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}

/**************************************************************
    Problem: 1126
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:4 ms
    Memory:1708 kb
****************************************************************/

你可能感兴趣的:(C/C++,OJ,codeup)