Ski video{SilverQuestion3}
[Problem description]
The TV timetable for the Winter Olympics contains N (1 <= n <= 150) programs, each of which has a start time and end time. Farmer John has two video recorders. Please calculate the maximum number of programs he can record.
[File input]
The first line is an integer n.
The next n rows have two integers in each row, indicating the start time and end time of a program. The value range is 0 .. 1,000,000,000.
[File output]
An integer that indicates the maximum number of programs that can be recorded.
[Input example]
6
0 3
6 7
3 10
1 5
2 8
1 9
[Output example]
4
[Example]
1st recorded programs 1 and 3, and 2nd recorded programs 2 and 4.
Idea: At first I thought I would use DP twice. The first DP would get the optimal solution, then I would delete the optimal solution, and then DP. the last result would be the answer, but this question may be covered by the two videos, in this way, two DP statements need to be judged, and then the sentence is annoying, so it will not be scored, with only 70 points. Later I learned that this question could have been greedy. I was also drunk, and sorted the end time first, and then recorded the end time of the last two recorders, if a video can only be recorded by one camera, you can record it. If both video recorders can record this video, select the one with the last two video recorders to record the video (as for why, let alone, think about it ). The following code
1 var 2 S, T: array [0 .. 150] of longint; 3 I, n, T1, T2, Ans: longint; 4 5 procedure openit; 6 begin 7 assign (input, 'Recording. in '); assign (output, 'Recording. out'); 8 reset (input); rewrite (output); 9 end; 10 11 procedure closeit; 12 begin13 close (input); close (output); 14 end; 15 16 procedure qsort (L, R: longint); 17 var I, j, M, temp: longint; 18 begin19 I: = L; J: = r; m: = T [(L + r) Div 2]; 20 repeat21 while T [I] <m do Inc (I ); 22 while T [J]> M do Dec (j); 23 if not (I> J) then24 begin25 temp: = s [I]; s [I]: = s [J]; s [J]: = temp; 26 temp: = T [I]; t [I]: = T [J]; t [J]: = temp; 27 Inc (I); Dec (j); 28 end; 29 until I> J; 30 if l <j then qsort (L, J ); 31 if I <r then qsort (I, R); 32 end; 33 34 35 begin36 openit; 37 readln (n); 38 for I: = 1 to n do readln (s [I], t [I]); 39 qsort (1, N); // the end time of the fast sorting is 40 T1: = 0; t2: = 0; ans: = 0; 41 for I: = 1 to n do42 begin43 if (s [I]> = T1) and (S [I]> = t2) then44 begin45 Inc (ANS); 46 If T1> T2 then T1: = T [I] 47 else t2: = T [I]; 48 continue; 49 end; 50 if s [I]> = T1 then begin Inc (ANS); T1: = T [I]; end; 51 if s [I]> = t2 then begin Inc (ANS); t2: = T [I]; end; 52 end; 53 writeln (ANS); 54 closeit; 55 end.
Usaco 2014 Jan ski video {silver Question 3}