[Explanations] - hdu this summer without AC [greedy] [sort] structure

## This summer is not AC

"This summer without AC?"
"Yes." "What
are you doing?"
"Watching the World Cup ah, stupid!"
"@ # $% ^ & *% ..."
Indeed, the World Cup to the fans of the festival also came , it is estimated will put aside a lot of ACMer 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 (Never forget that concerned about national affairs), is 6 + 7, Super girls, and Wang Xiaoya of "happy dictionary" and so on, assuming 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

Code

#include <stdio.h>
struct node{
    int num1;
    int num2;
}arr[105];
int main(void)
{
    int t, n, i, j, k;
    while(scanf("%d",&n) != EOF){
        if(n == 0) break;
        for(i = 0; i < n; i ++){
            scanf("%d%d",&arr[i].num1,&arr[i].num2);
        }
        for(i = 1; i < n; i ++){
            for(j = 0; j < n - i; j ++){
                if(arr[j].num2 > arr[j + 1].num2){
                    t = arr[j].num2;
                    arr[j].num2 = arr[j + 1].num2;
                    arr[j + 1].num2 = t;
                    t = arr[j].num1;
                    arr[j].num1 = arr[j + 1].num1;
                    arr[j + 1].num1 = t;
                }
                if(arr[j].num2 == arr[j + 1].num2){
                    if(arr[j].num1 < arr[j + 1].num1){
                        t = arr[j].num1;
                        arr[j].num1 = arr[j + 1].num1;
                        arr[j + 1].num1 = t;
                        t = arr[j].num2;
                        arr[j].num2 = arr[j + 1].num2;
                        arr[j + 1].num2 = t;
                    }
                }
            }
        }
        for(i = 1, t = arr[0].num2, k = 1; i < n; i ++){
            if(arr[i].num1 >= t){
                t = arr[i].num2;
                k ++;
            }
        }
        printf("%d\n",k);
    }
    return 0;
}

annotation

This question is used in the greedy algorithm and structure sorting, greedy algorithm that is locally optimal choice at every step to achieve the overall optimum.

Note that these, in the exchange of two structures, to teach the whole exchange, you need all this small portion of the structure are exchanged.

Published 34 original articles · won praise 2 · Views 931

Guess you like

Origin blog.csdn.net/Kapo1/article/details/103498499