We know that the principle of greedy algorithm is that when solving the problem, always make the best choice at present. In other words, not considering the overall optimality, he makes only the local optimal solution in a certain sense. The greedy algorithm can not get the whole optimal solution for all the problems, but it can produce the whole optimal solution or the approximate solution of the whole optimal solution for many problems with wide range.
Characteristics: Greedy algorithm uses top-down, iterative method to make successive greedy choice, every time greedy choice will be asked to simplify the problem as a smaller sub-problem, through each greedy choice, can get an optimal solution, although each step to ensure that the local optimal solution can be obtained, But the resulting global solution is sometimes not necessarily optimal, so the greedy method does not backtrack. The problem that can be solved by greedy algorithm generally has two important characteristics: greedy choice property and optimal substructure property.
Title: Give a set of activities that tell you the start and end times of each activity, and ask for the number of activities you can participate in or the starting and ending time of the activity
Greedy algorithm ideas:
Use two array s,f to store the start and end time of the activity, respectively, according to the activity's ending time to do a non-reduced activity sequence, the same activity starting time list also to do the corresponding adjustment, here Bo Master is through the bubble sort synchronous Exchange, example: Activity (1,4) (2,3) (3,5) then we get the
s = [2,1,3]f = [3,4,5]
Determines whether the two activities are compatible by comparing the start time of the next activity with the end time of the previous activity, if the start time is greater than the end time, then compatible, and vice versa, the code follows # Sort the end time with a bubbling sort, and get a list of the corresponding start time
def bubble_sort (s,f): for I in range (Len (f)): for J in Range (0,len (f)-i-1): if F[J] > f[j+1]: f[j], f[ J+1] = F[j+1],f[j] s[j],s[j+1] = s[j+1],s[j] return s,f def greedy (s,f,n): a = [True for x in range (n)] #初 Start Selection First activity j = 0 for i in range (1,n): #如果下一个活动的开始时间大于等于上个活动的结束时间 if s[i] >= f[j]: a[i] = true< C13/>j = i else: a[i] = False return a n = Int (input ()) arr = input (). Split () s = []f = []for ar in arr:
ar = ar[1:-1] start = Int (Ar.split (', ') [0]) end = Int (Ar.split (', ') [1]) s.append (start) F.append (end) s,f = Bubble_sort (s,f) A = greedy (s,f,n) res = []for k in range (len (A)): if a[k]: res.append (' ({},{ }) '. Format (S[k],f[k]) print (". Join (RES))
Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!
Related reading:
How PHP implements the stack data structure and the code example of the brace matching algorithm
The simplest string matching algorithm in PHP, PHP matching algorithm _php tutorial
The simplest tutorial for string matching algorithms in PHP