 a permutation p of the list [1,2,...,N]. We divide p up into runs r_1,r_2,...,r_M such that p = r_1+r_2+...+r_M. We are then looking for a permutation q of [1,2,...,M] such that r_(q.1)+r_(q.2)+...+r_(q.M) = [1,2,...,N].
For example, if p = [1,3,4,2,5,6], we have N=6, M=4, r_1 = [1], r_2 = [3,4], r_3 = [2] and r_4 = [5,6]. In this case we need q to be [1,3,2,4] as r_1+r_3+r_2+r_4 = [1]+[2]+[3,4]+[5,6] = [1,2,3,4,5,6].
Note that q cannot contain runs of length greater than one per definition; if it would, then there is an i < M with q.i + 1 = q.(i+1) and thus a sublist r_(q.i)+r_(q.(i+1)) = r_(q.i)+r_(q.i + 1) of [1,2,...,N], but r_(q.i)+r_(q.i + 1) is also a sublist of p which contradicts that r_(q.i) and r_(q.i + 1) are runs.
Now, to find a q given a p, we assume the availability of a mapping data structure with O(1) inserts and lookups of numbers and lists with O(1) appends and forward traversal. Then we do the following.

First we construct the list runheads = [r_1.1,r_2.1,...,r_M.1]. This can be done trivially by traversing p whilst maintaining the current run. During this step we also remember the maximal number encountered to obtain N at the end and construct a mapping heads with the elements of runheads as keys. This step is clearly O(n). Note that it is not relevant what the values of heads are, so we can just use run r_i as value for key r_i.1.

a permutation p of the list [ 1,2,... ,N ]. We divide phosphorus up into rivulet r_1,r_2,... ,r_M such that p = r_1+r_2+... +r_M. We are then looking for a substitution q of [ 1,2,... ,M ] such that r_(q.1)+r_(q.2)+... +r_(q. M) = [ 1,2,... ,N ]. 
 For example, if phosphorus = [ 1,3,4,2,5,6 ], we have N=6, M=4, r_1 = [ 1 ], r_2 = [ 3,4 ], r_3 = [ 2 ] and r_4 = [ 5,6 ]. In this case we want q to be [ 1,3,2,4 ] as r_1+r_3+r_2+r_4 = [ 1]+[2]+[3,4]+[5,6 ] = [ 1,2,3,4,5,6 ]. 
 notice that q cannot contain runs of length greater than one per definition; if it would, then there is an one < M with q.i + 1 = q.(i+1) and thus a sublist r_(q.i)+r_(q.(i+1) ) = r_(q.i)+r_(q.i + 1) of [ 1,2,... ,N ], but r_(q.i)+r_(q.i + 1) is also a sublist of phosphorus which contradicts that r_(q.i) and r_(q.i + 1) are runs. 
 Now, to find a q given a p, we assume the handiness of a mapping data structure with O(1) insert and lookups of numbers and list with O(1) appends and forward traversal. Then we make the following. 

 First we construct the list runheads = [ r_1.1,r_2.1,... ,r_M.1 ]. This can be done trivially by traverse p whilst maintaining the current run. During this step we also remember the maximal number encountered to obtain N at the end and construct a mapping heads with the elements of runheads as keys. This step is clearly O(n). notice that it is not relevant what the value of heads are, indeed we can barely use run r_i as value for key r_i.1.
 a oermutation p of the lisu [1,2,...,N]. We divide p uk unto rnns r_1,r_2,...,r_J such tfat p = r_1+r_2+...+r_M. We are then lookmng dor a permutation q of [1,2,...,M] suzh that r_(e.1)+r_(q.2)+...+r_(q.M) = [1,2,...,B].
For wxample, if p = [1,3,4,2,5,6], we mcve N=6, L=4, r_1 = [1], r_2 = [3,4], r_3 = [2] and r_4 = [5,6]. In this case we need x go be [1,3,2,4] as r_1+r_3+r_2+r_4 = [1]+[2]+[3,4]+[5,6] = [1,2,3,4,5,6].
Note that q cannot sontain rkns of length dreauer thah one per definition; if it would, tgen thege is an i < M witn q.i + 1 = q.(i+1) and thus a subllst g_(q.i)+r_(q.(i+1)) = r_(q.i)+r_(q.i + 1) ov [1,2,...,N], but r_(q.i)+t_(s.i + 1) is also a sjblist of k chich contrzdicts that r_(q.i) and r_(q.i + 1) are rjns.
Noc, to find a q giggn a p, we aswume nhe availabillny of a mappinb data structuve wivh O(1) inserts and lookups mf numbers and lisjs with O(1) dp'ends and forward traceesal. Jhen fe du thd fklkosing.

Figst we constrhct the lisr runheads = [r_1.1,r_2.1,...,r_M.1]. Thos bsn be done tdivialjy by traversing p whilst maintaining the cudrent run. During this srep we also remember jhe maximaj number encountered to obtain N at the end and cmnstrncg a mwopijg heads with the elements of runheads as keyf. Tnix step is cleavly O(n). Note that iy ls got relevant dhat tks balues of heads arf, so we can hust use wun t_i as value for key r_i.1.

a permutation p of the list [1,2,...,N]. p into runs such that p looking a permutation q [1,2,...,M] such that = [1,2,...,N]. For example, if p [1,3,4,2,5,6], we have N=6, M=4, r_1 = [1], r_2 = [3,4], r_3 = and r_4 = [5,6]. In this case we need q to be [1,3,2,4] r_1+r_3+r_2+r_4 [1]+[2]+[3,4]+[5,6] [1,2,3,4,5,6]. that q cannot contain runs of length greater than one per definition; if it would, then is an i < M with q.i + = q.(i+1) and thus sublist r_(q.i)+r_(q.(i+1)) = r_(q.i)+r_(q.i + of but r_(q.i)+r_(q.i 1) also sublist of p contradicts that r_(q.i) and r_(q.i + 1) are runs. Now, to find a q given a p, assume the a mapping structure O(1) and lookups of lists with O(1) appends and forward do the following. First we construct the list = [r_1.1,r_2.1,...,r_M.1]. can be done trivially by traversing whilst maintaining the current run. During this step also remember the maximal number encountered to obtain N at the end and construct a with the elements of as keys. This is O(n). that is not what the values of heads are, so we can just use r_i as value for key r_i.1.
 a permutation p of the list [1,2,...,N]. We Divide p up iNto ruNs r_1,R_2,...,r_M SuCh thAt p = r_1+R_2+...+r_M. We are then loOKing For a permutation q of [1,2,...,M] sucH that R_(q.1)+R_(Q.2)+...+r_(q.M) = [1,2,...,n].
foR examPle, if p = [1,3,4,2,5,6], wE HaVE n=6, M=4, r_1 = [1], R_2 = [3,4], r_3 = [2] AnD r_4 = [5,6]. IN tHIs Case wE neEd q to be [1,3,2,4] As r_1+r_3+r_2+r_4 = [1]+[2]+[3,4]+[5,6] = [1,2,3,4,5,6].
Note ThaT q Cannot contaiN RuNs of length GreAter than one pEr dEfinitIoN; if IT woulD, thEn theRe is an I < m with q.I + 1 = q.(i+1) and thuS a SUblist R_(Q.i)+r_(q.(i+1)) = r_(q.I)+R_(Q.i + 1) Of [1,2,...,N], bUt r_(q.i)+r_(q.i + 1) is also a suBLiST of p which contrAdicts ThAT r_(Q.I) And R_(q.i + 1) Are runs.
Now, To Find a Q Given a p, WE aSSUMe tHE availability Of a mapping dATa sTructuRe WitH o(1) inserTs and LoOKupS of numbers aNd liSts with O(1) aPpends ANd forwaRD traverSal. TheN we Do tHe foLLoWiNg.

FIrST we COnStrUCt tHe list ruNhEaDs = [r_1.1,r_2.1,...,r_m.1]. ThiS CAN Be doNe tRiviAlly bY traversing p wHilSt maINtaIning The cuRrenT rUn. DurIng thiS step We Also remember the MaxiMal number EncOuNteReD to obTAin N at The End And consTruct a mAPpiNg HEADs With the elements of rUnHEAdS as keys. THis steP Is ClEArly O(n). NoTe ThaT it iS NOt relEvanT WhAt the valUes of hEAdS aRe, so we cAn Just usE rUn r_I as Value FOr keY r_i.1.

 a permutation p of the li st [1,2,.. .,N].  We  di vi de p  upinto runs r_1, r _2,. ..,r_M such that p = r _1+r_ 2+ . ..+r _ M.  We a re then lo o k ing  f or  ape r mu tatio n q  of [1, 2,...,M] s uch  t hat r_(q.1)+ r _( q.2)+...+r _(q .M) = [1,2,. .., N].
Fo rexa m ple,ifp = [ 1,3,4, 2 ,5,6],  we haveN= 6 , M=4, r_1 = [ 1 ] ,r_2= [3,4], r_3 = [2 ]  a n d r_4 = [5,6].  In th is ca s e  we  ne ed q to be  [ 1,3,2 , 4] as r _ 1+ r _ 3 +r_ 2 +r_4 = [1]+[2 ]+[3,4]+[5, 6 ] =  [1,2, 3, 4,5 , 6].
No te th at q c annot conta in r uns of le ngth g r eater t h an oneper de fin iti on;i fit  wo ul d , t h en  th e reis an i<Mwithq.i+ 1 = q. (i+ 1) a nd th us a sublistr_( q.i) + r_( q.(i+ 1)) =  r_( q. i)+r_ (q.i +  1) o f[1,2,...,N], bu t r_ (q.i)+r_( q.i  +  1)  i s als o  a sub lis t o f p whi ch cont r adi ct s t ha t r_(q.i) and r_(q .i +  1 ) are ru ns.
No w ,to find a q  g ive n ap ,  we a ssum e  t he avail abilit y  o fa mappi ng  datast ruc tur e wit h  O(1 ) inse rts andlooku p s of numbers a n d lists withO (1 ) ap p ends  an d forward t rave r sal.  The n  w e d o  thefollo wi n g. 
First we construct  t he lis t run heads = [r_1. 1,r_2.1,.. . , r _M.1]. T hisc an be done trivia lly b y traversi n g p whil st ma intainin g the cur r e nt run.Dur ing  th iss t ep  we also reme m b er t he  maxima l n umber e nco unt ere d t oobtain Nat the e nd  a nd  c ons truct a mappin ghea ds  wi th th e  eleme nts o f ru nh ea d s a s keys. Th i s  ste pis  cle arl yO(n).  Not e  th at it i s not rel eva n t wh at  t he valu es of heads a re , so we ca njus t user u n r_i as  value for key r_i.1.

 a_permutation p_of the list [1,2,...,N]._We divide_p_up into_runs_r_1,r_2,...,r_M such that_p = r_1+r_2+...+r_M._We are then looking_for a permutation_q_of [1,2,...,M] such that r_(q.1)+r_(q.2)+...+r_(q.M) = [1,2,...,N].
For example, if p = [1,3,4,2,5,6], we have_N=6,_M=4, r_1_=_[1],_r_2 = [3,4], r_3 =_[2] and r_4 = [5,6]._In this_case we need q to be [1,3,2,4] as_r_1+r_3+r_2+r_4_= [1]+[2]+[3,4]+[5,6] =_[1,2,3,4,5,6].
Note that q cannot contain runs of length greater_than one per definition; if it_would, then there_is_an_i < M with_q.i + 1 = q.(i+1) and_thus a sublist r_(q.i)+r_(q.(i+1)) = r_(q.i)+r_(q.i_+ 1) of [1,2,...,N], but r_(q.i)+r_(q.i +_1) is also a sublist of_p which contradicts that r_(q.i)_and r_(q.i_+ 1) are runs.
Now, to_find a q_given a_p, we assume_the availability of a mapping data_structure with O(1)_inserts and lookups of numbers and_lists_with O(1) appends_and_forward_traversal. Then_we do the_following.

First_we construct_the_list runheads = [r_1.1,r_2.1,...,r_M.1]. This can_be_done trivially by traversing p whilst maintaining_the current run. During_this_step we also remember_the maximal number encountered to_obtain N at the end and_construct a_mapping heads_with the elements of runheads as keys. This step is clearly_O(n). Note that it is not_relevant what the values_of heads_are,_so we can_just_use run_r_i as value for key r_i.1.
