This summer without AC HDU - 2037 (interval scheduling)

"This summer without AC?" 
"Yes." "What 
are you doing?" 
"Yeah watching the World Cup, stupid!" 
"@ # $% ^ & *% ..." 

Indeed, the World Cup to the fans of the festival also it came, estimated that many ACMer will put aside the computer, toward the television. 
As fans, we want to see as much of the full game, of course, as a new era of good young people, you must also look at some other programs, such as news network, is 6 + 7, super girl, and Wang Xiaoya of "Happy Dictionary "Wait, assume that you already know you like to watch the broadcast schedule of all TV programs, you'll arrange it? (Goal is to see as much of the full program) 

Input

Input data comprising a plurality of test example, the first line of each test case only one integer n (n <= 100), represents the total number of programs you like, then n rows, each row comprising two data Ti_s, Ti_e (1 <= i <= n), represent the i-th program start and end times, in order to simplify the problem, each time with a positive integer. n = 0 represents the input end without processing. 

Output

For each test case, the number of outputs can see the complete TV program, the output of each test case in a separate line.

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

Ideas:

This problem belongs to the interval scheduling, using the greedy method, according to the program end time order from small to large, if the end of the same time, press the start time of ordering, the more the later start time priority (description of the program accounts for a minimum length of time), this can be as much as possible see most programs.

C ++ code:

#include <iostream>
//同区间调度
using namespace std;
class Node
{
    public:int sp;
    public:int ep;

};
int main()
{
    int n;
    Node node[150];
    cin>>n;
    while(n)
    {
        for(int i=0; i<n; i++)
        {
            cin>>node[i].sp;
            cin>>node[i].ep;
        }
        for(int i=0;i<n-1;i++)    //冒泡排序
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(node[j].ep>node[j+1].ep||(node[j].ep==node[j+1].ep&&node[j].sp<node[j+1].sp))
                {
                    swap(node[j].ep,node[j+1].ep);
                    swap(node[j].sp,node[j+1].sp);
                }
            }
        }
        int ans=1;
        int e=node[0].ep;     //e用来维护已经安排收看的所有节目的结束时间
        for(int i=1;i<n;i++)
        {
            if(node[i].sp>=e)
            {
                e=node[i].ep; //若该节目能被安排收看,更新e的值
                ans++;
            }
        }
        cout<<ans<<endl;
        cin>>n;
    }
    return 0;
}

 

Published 35 original articles · won praise 2 · Views 1387

Guess you like

Origin blog.csdn.net/weixin_41001497/article/details/88667726