[Algorithm] greedy _ program scheduling issues

 

 

Problem Description
  "This summer do not learn?"
  "Really? What are you going to do?"
  "Yeah watch TV!"
  "You see so many TV shows finished yet?"
  "Yes Oh, that's a good arrange the program."
  Indeed, the summer came, drama holiday season has come, estimated that many TV fans will put aside their studies toward the TV. As TV fans, we want to see as much as possible in a day full of drama. 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), the good times flowing, Changan 12 hour, petition orders, and Wang Xiaoya "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)
 
 
Entry
  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.
 
 
Export
  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
 
 

Problem-solving ideas

  It gives a total length that is determined interval, and then enter a different subintervals, seeking in this subinterval section which contains a total of up to disjoint number.

  You can think of the most exceptional circumstances, that is, given these sub-intervals now do not intersect, such as

  ----- (1,3)                                                                       1

             ---------- (2,4)                                                           2

                                --------(5,7)                                           3

                                               -----------------(8,10)               4

  0-10 can be seen in this general range which, max = a total of 4 disjoint subintervals, from general to specific, if we move to the left section 4, then the number of disjoint sub-intervals becomes max 3, 1 is reduced, to give the same reason, if the mobile subintervals other, will result in the value max is smaller (which results in movement of the section and intersecting another subranges) or unchanged (the section does not move will lead to intersect with the other sections), then we can conclude that the optimal solution of the case under normal circumstances should be the most similar to the special circumstances of this form. I.e., the case should be the optimal solution, the starting point is greater than the second end of the first sub-interval sub-interval (as determined by this condition can traverse), thus ensuring that they are no intersections, so it is required subintervals orderly, placing them in the small to large end of the sort, the minimum interval may be obtained from the end of the above examples the optimal solution must be contained in the inside, (because it is the end of the latest start of the earliest, after a certain row sorted the range of the far left).

 

  

 

#include <stdio.h>
 struct Time 
{ 
int Ti_s;     // start time 
int Ti_e;     // End Time 
};
 int main () 
{ 
    int A, I, J, COUNT; 
    Time Time [ 100 ], TEMP;     // TV shows the total number of structures to represent 
    the while (Scanf ( " % D " , & A) = the EOF && A =!! 0 ) 
    { 
    for (I = 0 ; I <A; I ++ ) 
    Scanf ( " % D% D " , & Time [ I] .Ti_s, & time [I] .Ti_e); 
 
     // ascending sorted according to the program end time
    for (I = 0 ; I <A; I ++ )
          for (I = J + . 1 ; J <A; J ++ ) 
         { 
            IF (Time [I] .Ti_e> Time [J] .Ti_e || (Time [I] .Ti_e Time == [J] && .Ti_e Time [I] .Ti_s> Time [J] .Ti_s)) 
            { 
                TEMP = Time [I]; 
                Time [I] = Time [J]; 
                Time [J] = TEMP; 
            } // TEMP structure holds its start time end time of a program 
         } 
 
        for (I = . 1 , J = 0 , = COUNT . 1 ; I <a; I ++ ) 
        {
            IF (Time [I] .Ti_s> = Time [J] .Ti_e) 
            { 
            COUNT ++ ; 
            J = I; 
               } 
        } // greedy algorithm, a first program certainly depends sorted, and then successively calculates a next program start time greater than or equal to the end time of the program number of a program 
        the printf ( " % D \ n- " , COUNT); 
    } 
    
 return  0 ; 
}
View Code

 

 

Guess you like

Origin www.cnblogs.com/1138720556Gary/p/11371910.html