DeCantor Expansion (逆康托展开)

Background\text{Background}

The \text{The }Listen&Say Test will be hold on May 11, so I decided to fill my blog \text{Test will be hold on May 11, so I decided to fill my blog }
with English words until that day.\text{with English words until that day.}

Problem\text{Problem}

There goes a problem.\text{There goes a problem.}

You’ve got 2 intergers N,k. Please calculate the kth permutation of k[i,n].\text{You've got 2 intergers }N,k\text{. Please calculate the }k\text{th permutation of }\forall k\in[i,n].

Solution\text{Solution}

It’s easy to know that we can got it by Depth-first Search, \text{It's easy to know that we can got it by Depth-first Search, }but its Time complexity is O(n!).\text{but its Time complexity is }O(n!).

DeCantor Expansion is a algorithm which can solve problems like these calculating the kth permutation\text{is a algorithm which can solve problems like these calculating the }k\text{th permutation}in O(nlogn) with heap optimization.\text{in }O(n\log n)\text{ with heap optimization.}


Let’s explain how it works in a simple example. Set N=5,k=61, the answer is a[].\text{Let's explain how it works in a simple example. Set }N=5,k=61,\text{ the answer is }a[].

1.Let 61 / 4! = 2 ... 13, it shows that there’re 2 numbers behind a[1] are smaller than a[1].\text{1.\quad Let 61 / 4! = 2 ... 13, it shows that there're 2 numbers behind }a[1]\text{ are smaller than a[1].}
Therefore, a[1]=3;\text{Therefore, }a[1]=3;

2.Let 13 / 3! = 2 ... 1, it shows that there’re 2 numbers behind a[2] are smaller than a[2].\text{2.\quad Let 13 / 3! = 2 ... 1, it shows that there're 2 numbers behind }a[2]\text{ are smaller than a[2].}
Therefore, a[2]=4;\text{Therefore, }a[2]=4;

3.Let 1 / 2! = 0 ... 1, it shows that there’re 0 number behind a[3] are smaller than a[3].\text{3.\quad Let 1 / 2! = 0 ... 1, it shows that there're 0 number behind }a[3]\text{ are smaller than a[3].}
Therefore, a[3]=1;\text{Therefore, }a[3]=1;

4.Let 1 / 1! = 1 ... 0, it shows that there’re 1 number behind a[4] are smaller than a[4].\text{4.\quad Let 1 / 1! = 1 ... 0, it shows that there're 1 number behind }a[4]\text{ are smaller than a[4].}
Therefore, a[4]=5;\text{Therefore, }a[4]=5;


Therefore, a[5]=2,a[]={3,4,1,5,2}.\text{Therefore, }a[5]=2, a[]=\{3,4,1,5,2\}.

Summary\text{Summary}

i[1,n1], let k / (n1)!, the answer you’ve got is the number of interger j[i+1,n] which has a[j]<a[i]. And let k equals to the remainder.\forall i\in[1,n-1],\text{ let }k\ /\ (n-1)!\text{, the answer you've got is the number of interger }\newline j\in[i+1,n]\text{ which has }a[j]<a[i].\text{ And let }k\text{ equals to the remainder.}

The End\text{The End}

Reference material\text{Reference material}

posted @ 2019-04-25 11:41  TeacherDai  阅读(135)  评论(0)    收藏  举报