For the search for the number of the ordinal count of I, the form can be defined as follows:
The most primitive way to do this is to sort the number of 1~n from small to large, and then output the number of I. With fast-track, the time complexity is O (nlogn), but we can actually optimize for fast sorting.
The quick sort is a two-point idea, sorting each side of the parting, but when we ask for the number of I, what side of the beat do we need to sort the other side of the fast line? Needless to tell, this is not necessary, so this is the optimization--only one side of the queue is processed.
Set to A[l. R] to divide. First of all, according to the central idea of the fast line, select (l+r) Div 2 as the "Fulcrum" to divide the two sides. To A[l. R] After dividing, we should get a midnum point, that is, the dividing point, namely a[l. All elements of midnum] are less than A[MIDNUM+1..R]. We then continue to divide the side of the current point I, until l=r, output A[L], is the number of the first order of our statistics.
So how until we are present in L.. Which side of Midnum is the first point I want to find in the R interval?
We set up k=midnum-l+1. Represents A[l. Midnum], if the current required i<=k is represented in L. Midnum to--dfs (l,midnum,i) in the interval, and vice versa in MIDNUM+1..R, DFS (MIDNUM+1,R,I-K).
Code:
Type arr=array[1..100] of Longint;var n,k,i:longint; A:arr;function Midnum (var A:arr; l,r:longint): Longint;var x,t:longint;begin x:=a[(l+r) Div 2]; While L<r does begin while A[l]<x do Inc (L); While A[r]>x do Dec (r); If L<r then if A[L]<>A[R] then BEGIN T:=A[L]; A[L]:=A[R]; a[r]:=t; END ELSE BEGIN Inc (L); Dec (r); End End Midnum:=r;end;procedure FS (L,r,i:longint), var p,k:longint;begin if L=r then BEGIN writ ELN (A[l]); Halt End ELSE begin K:=midnum (a,l,r)-l+1; If I<=k then FS (l,p,i) Else FS (p+1,r,i-k); End;end;begin readln (n,k); For I:=1 to n do read (A[i]); FS (1,n,k); end.
By this simple example we can extrapolate, for example, we can use the above algorithm when we find the median, and we can use it when we find the median of the right number.
A study on the search for the number of the first order statistics.