This question gives you a set of time ranges to find the maximum time span and the maximum time interval. The question seems quite simple, but if you do not consider it, you will find that there are many situations for Interval processing. That is, the clubProgramMultiple judgment statements may appearCodeIt becomes very difficult to write and error-prone.
My first solution
I used a strange data structure that combines linked lists and linear tables. A linear table is used to represent a timeline, and a linked list is used to represent spans of different time periods.
Take the sample data as an example.
300 1000700 12001500 2100
Regardless of the start and end of the time, I put them together on a number axis and then sorted to get a timeline.
300 700 1000 1200 1500 2100
This is the first step. My second step is to traverse the timeline. If a value is the start point of a time, it will get its end point. Create a linked list from this start point to its end point.
For example, the time range is or. My program will handle it like this
300 --> 700 --> 1000
Then to 700, after connecting from 700 to 1200, and then 1500 (1000 is the end of a time period), a time period will become a linked list:
300 --> 700 --> 1000 --> 1200 1500 --> 2100
The chain with the longest time span is what you want (not necessarily the longest linked list ).
The advantage of this solution is that there is no need to judge the intersection of time spans, as long as they are concatenated, it is equivalent to repeatedly painting the color on the number axis, and finally find the largest color.
However, the disadvantage is that the worst case for establishing a linked list is O (n ^ 2 ).
100 1000200 900300 800400 700500 600
However, the data is relatively random in most cases, so I personally estimate that the average span of each time period is about logn, so the code runs very fast during testing.
View code
1 # Include <iostream>
2 # Include <fstream>
3 # Include < String >
4 # Include <map>
5 # Include <vector>
6 # Include <algorithm>
7
8
9 Using Namespace STD;
10
11 Struct Node {
12 Int Value;
13 Struct Node * next;
14 Node ( Int Value ){ This -> Value = value; This -> Next = NULL ;}
15 };
16
17 Void P_axis (vector <node> & axis ){
18 For ( Int I = 0 ; I <axis. Size (); ++ I ){
19 Cout <axis [I]. value;
20 If (Axis [I]. Next! = NULL)
21 Cout < " --> " ;
22 Else
23 Cout < " " ;
24 }
25 Cout <Endl;
26 }
27
28 Bool Node_cmp (node N1, node N2 ){
29 Return N1.value <n2.value;
30 }
31
32 Int Index (vector <node> & axis, Int Value ){
33 For ( Int I = 0 ; I <axis. Size (); ++ I)
34 If (Axis [I]. value = value)
35 Return I;
36 Return - 1 ;
37 }
38
39
40 Int Main (){
41 Ofstream fout ( " Milk2.out " );
42 Ifstream fin (" Milk2.in " );
43
44 Int Len = 0 ;
45 Fin> Len;
46
47 Map < Int , Int > Pairs;
48 Vector <node> node_axis;
49
50 Int Start = 0 , End = 0 ;
51 Map < Int , Int >:: Iterator ITER;
52 For ( Int I = 0 ; I <Len; ++ I ){
53 Fin> Start> end;
54 Iter = pairs. Find (start );
55 If (Iter = pairs. End ()){
56 Pairs [start] = end;
57 } Else {
58 Pairs [start] = max (pairs [start], end );
59 }
60 Node_axis.push_back (node (start ));
61 Node_axis.push_back (node (end ));
62 }
63
64 // Printf ("Len: % d, node_axis: % d, MAP: % d \ n", Len, node_axis.size (), pairs. Size ());
65 Sort (node_axis.begin (), node_axis.end (), node_cmp );
66
67 // P_axis (node_axis );
68
69
70 For ( Int I = 0 ; I <node_axis.size (); ++ I ){
71 Node * cur = & node_axis [I];
72
73 Int Value = cur-> value;
74
75 Map < Int , Int >:: Iterator iter = pairs. Find (value );
76 If (Iter = pairs. End ())
77 Continue ;
78 // Cout <I <":" <"Start:" <value <", end:" <(* ITER). Second <Endl;
79
80 Int End = (* ITER). Second;
81 Int J = I;
82 Do {
83 ++ J;
84 Cur-> next = & node_axis [J];
85 Cur = & node_axis [J];
86 } While (Node_axis [J]. value! = END );
87 }
88
89 // P_axis (node_axis );
90
91 Int Max_mk = 0 , Max_nomk = 0 , I = 0 ;
92
93 Node * end_node = NULL;
94 While (I <node_axis.size ()){
95 Node * Start = & node_axis [I];
96 If (End_node ){
97 Int No_interval = start-> value-end_node-> value;
98 Max_nomk = no_interval> max_nomk? No_interval: max_nomk;
99 }
100
101 End_node = start;
102 While (End_node-> next! = NULL ){
103 End_node = end_node-> next;
104 I + = 1 ;
105 }
106 Int Interval = end_node-> value-start-> value;
107 Max_mk = interval> max_mk? Interval: max_mk;
108
109 I + = 1 ;
110 }
111
112 Fout <max_mk < " " <Max_nomk <Endl;
113
114
115 Fin. Close ();
116 Fout. Close ();
117 Return 0 ;
118 }
Simplified Solution
After solving the problem, I saw the above prompt in the analysis and suddenly thought that after sorting, merging has become very easy. The idea is as follows:
1. First, sort the given time period by the initial time]
2. Traverse from the leftmost, and obtain two adjacent periods a and B each time. (Note: the start time of B is already on the right of)
There are three scenarios:
1) Update A. End to B. End.
2) We start another time period because a does not need to be merged.
3) skip B directly
After such merging traversal, we get a timeline of the merged time period, where each time period does notIntersection. This makes it very convenient for us to evaluate.
The Code is as follows:
View code
# Include <iostream>
# Include <fstream>
# Include < String >
# Include <map>
# Include <vector>
# Include <algorithm>
Using Namespace STD;
Struct Interval {
Int Start;
Int End;
Interval ( Int Start, Int End ){ This -> Start = start; This -> End = end ;}
Interval ( Const Interval & Val ){ This -> Start = Val. Start; This -> End = Val. end ;}
Int Span (){ Return ( This -> End- This -> Start );}
};
Bool Operator <( Const Interval & val1, Const Interval & val2 ){ Return Val1.start <val2.start ;}
Int Main (){
Ofstream fout ( " Milk2.out " );
Ifstream fin (" Milk2.in " );
Int Len = 0 ;
Fin> Len;
Vector <interval> node_axis;
Int Start = 0 , End = 0 ;
For ( Int I = 0 ; I <Len; ++ I ){
Fin> Start> end;
Node_axis.push_back (interval (START, end ));
}
// Printf ("Len: % d, node_axis: % d, MAP: % d \ n", Len, node_axis.size (), pairs. Size ());
Sort (node_axis.begin (), node_axis.end ());
// P_axis (node_axis );
// Add a sentinel
Node_axis.push_back (interval ( 655534 ,655535 ));
Vector <interval> combined_axis;
Interval max_interval = node_axis [ 0 ];
For ( Int I = 1 ; I <node_axis.size (); ++ I ){
If (Node_axis [I]. Start> max_interval.end ){
Combined_axis.push_back (max_interval );
Max_interval = node_axis [I];
} Else If (Node_axis [I]. End> max_interval.end ){
Max_interval.end = node_axis [I]. end;
} Else {
// Filter this interval
Continue ;
}
}
Int Max_mk = combined_axis [ 0 ]. Span (), max_nomk = 0 ;
For ( Int I = 1 ; I <combined_axis.size (); ++ I ){
Max_mk = max (combined_axis [I]. span (), max_mk );
Max_nomk = max (combined_axis [I]. Start-combined_axis [I- 1 ]. End), max_nomk );
}
Fout <max_mk < " " <Max_nomk <Endl;
Fin. Close ();
Fout. Close ();
Return 0 ;
}