Description
Functioning of a scientific conference is usually divided into several simultaneous sections. For example, there is a section in parallel computing, a section on visualization, a section on data compression, and So on. Obviously, simultaneous work of several sections are necessary in order-to-reduce the time for scientific program of the CO Nference and to has more time for the banquet, tea-drinking, and informal discussions. However, it's possible that interesting reports be given simultaneously at different sections. A participant have written out the time-table of all the reports which is interesting for him. He asks you to determine the maximal number of reports he'll be able to attend.
Input
The first line contains the number 1≤N≤100000 of interesting reports. Each of the nextNlines contains, integersTsandTeSeparated with a space (1≤Ts<Te≤30000). These numbers is the times a corresponding report starts and ends. Time is measured in minutes from the beginning of the conference.
Output
You should output the maximal number of reports which the participant can attend. The participant can attend no, reports simultaneously and any, reports he attends must be separated by at least one Minute. For example, if a report ends at, the next report which can is attended must begin at + or later.
Sample Input
| input |
Output |
53 41 56 74 51 3 |
3 |
To find out the maximum number of disjoint intervals
The original source of the transmission door
Greedy:
For all the time to sort the end, because the sooner the end of the more can be added in, each step is the best
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;struct edge{ int b; int e;} A[100010];bool CMP (Edge I, Edge j) { if (i.e = = J.E) return I.B < J.B; return i.e < J.E;} int main () { int n; while (~SCANF ("%d", &n)) {for (int i = 1; I <= n; i++) scanf ("%d%d", &A[I].B,&A[I].E); Sort (a+1,a+n+1,cmp); int t = 0; int ans = 0; for (int i = 1; I <= n; i++) { if (a[i].b >= t) { t = a[i].e + 1; ans++; } } printf ("%d\n", ans); } return 0;}
Dp:
Ural1203--dpor Greedy--scientific Conference