"Problem description"
Now given n closed interval [ai,bi],1<=i<=n. These intervals can be expressed as a number of disjoint closed intervals. Your task is to find the scheme with the least interval in these representations. Your output should be sorted in ascending order of intervals. If you say two intervals [A, b] and [C, d] are in ascending order, then we have a<b<=c<=d.
"Sample Input"
5
5 6
1 4
10 10
4 {
8 10
"Sample Output"
1 4
5 10
"Problem-solving ideas"
This is titled Sdoi 2005 Day2 First question, is the variation of the interval coverage problem, in fact, after looking at the problem described may not understand the meaning of the topic, but after reading the sample will be found, in fact, will be some connected to the interval together, and finally from small to large output after each combined interval. Since small to large, that the first step is naturally the beginning of each interval to the key word from small to large sort, and then from the second interval, each interval and the previous selection of the interval to compare, if connected together, then merge the interval, otherwise, open a new interval, the interval will be deposited in a new interval. Note: In addition to determining whether to join together, but also to determine the size of the end point, if the end of a large number to save, otherwise ignore the interval.
"Code Implementation"
1 typeRec=Record2 A,b:longint;3 End;4 varC,d:Array[1..50000] ofRec;5 I,j,n,m,ans,r,w:longint;6 proceduresort (l,r:longint);7 var8 I,j,x:longint;9 Y:rec;Ten begin Onei:=l; Aj:=R; -x:=c[(L+r)Div 2].a; - Repeat the whileC[i].a<x Do - Inc (i); - whileX<c[j].a Do - Dec (j); + if not(I>J) Then - begin +y:=C[i]; Ac[i]:=C[j]; atc[j]:=y; - Inc (i); -j:=j-1; - End; - untilI>J; - ifL<j Then in sort (l,j); - ifI<r Then to sort (i,r); + End; - begin the READLN (n); * fori:=1 toN Do $ withC[i] DoPanax Notoginseng Readln (A, b); -Sort1, n); thew:=1;d [1].a:=c[1].a;d[1].b:=c[1].b; + fori:=2 toN Do A if(C[I].A<=D[W].B) and(C[I].B>=D[W].B) Then//determine if the interval is coincident, if coincident, update the end point thed[w].b:=c[i].b + Else - ifC[i].a>d[w].b Then//if not coincident, open a new interval $ begin $ Inc (W); -d[w].a:=c[i].a; -d[w].b:=c[i].b; the End; - fori:=1 toW DoWuyi withD[i] Do theWriteln (A,' ', b); - End.
Interval (Vijos 1439)