Title Description
In theory, Nova is a big idler, but every day there are a lot of things to do, big homework, innovation Cup, game, issue deceptive, Balabala ... However, the energy is limited, Nova June can only do one thing at a time, and can not be bent on two uses. Suppose there are now N jobs waiting for the Nova to finish, respectively, at the Si moment, at the end of Ti time, for each job can choose to do or not to do, but can not simultaneously select the time overlapping work (even the beginning of the moment and the end of the instant overlap is not allowed). Nova is naturally willing to do as much as possible, so how many things can be done?
Input
Multiple sets of test data (the number of data groups not more than 10), for each group of data, the first line of input a positive integer n, representing the optional number of work, followed by input n lines, two positive integers per line, representing the start time of the I work Si and the end time ti.
1<=n<=100000 | 1<=si<=ti<=10^9
Output
For each set of data, the output line is the maximum amount of work that can be done.
Input sample
51 32 54 76 98 10
Output sample
3
source of the topic: Http://biancheng.love/contest/23/problem/E/index
Problem-solving ideas: According to the activity end time sorting, first end of the row in the forefront, after the end of the row in the back.
Each time it is placed in a first-ended activity, the end time is compared to the start time of the next activity, and if the next activity starts after the end of the previous activity, then the activity is put in, and then in turn, until the last activity. The output is the maximum number of activities.
The code:
1#include <bits/stdc++.h>2 #defineMax_size 100103 using namespacestd;4 intOrder[max_size];5 6 structnode{7 intStart,end;8 intID;9 };Ten One node act[max_size]; A - BOOLCMP (node A,node b) - { the returna.end<B.end; - }; - - intMain () + { - intN; + while(~SCANF ("%d",&N)) A { at for(intI=0; i<n;i++) - { -scanf"%d%d",&act[i].start,&act[i].end); -act[i].id=i+1; - } -Sort (act,act+n,cmp); inorder[0]=0; - intNumber=1; to for(intI=1; i<n;i++) + { - if(act[i].start>=act[order[number-1]].end) theorder[number++]=i; * } $cout<<number<<Endl;Panax Notoginseng } -}
In the previous blog post, explain the difference between the schedule of activities:
1, before the blog on the DP-seeking activity arrangement, also described the use of greedy solution. But the previous question of the default input activity has been sorted by the activity end time, and now the problem is not sorted.
2, the previous activity can be the beginning of the next activity is just the end of the previous activity, but this topic requires not be able to overlap.
Here's a link to the previous blog: http://www.cnblogs.com/zpfbuaa/p/4951105.html
Busy Nova (event scheduling problem, greedy algorithm)