Let $n$ be a positive integer. There are $n$ purple and $n$ white cows queuing in a line in some order. Tim wishes to sort the cows by colour, such that all purple cows are at the front of the line. At each step, he is only allowed to swap two adjacent groups of equally many consecutive cows. What is the minimal number of steps Tim needs to be able to fulfill his wish, regardless of the initial alignment of the cows?
Problem
Source: MEMO 2022 T3
Tags: combinatorics
14.09.2022 18:13
Answer : $n$ First i will prove that regardless of the initial alignment of the cows Tim is able to fulfill his wish in at most $n$ moves. Induct on $n$. For $n=2 $ just take all initial alignments: $WWPP\to PPWW$,$WPWP \to WWPP\to PPWW$,$WPPW\to PWPW\to PPWW$, etc Suppose that it's true for $n=1,2,..,k-1$ and the alignment is $a_1a_2...a_{2n}$ If $a_1=P,a_2n=W$ then by induction hypothesis he can transform $a_2..a_{2n-1}$ to $\underset{n-1}{\underbrace{PP..P}}\underset{n-1}{\underbrace{WW..W}}$ using at most $n-1<n $ moves etc If $a_1=P,a_2n=P$ then let $k$ be the largest index such that $a_k=W$, since $a_1=P$ we have $k\geq n+1$ and so Tim can make the move $P... \underline{a_{2k-2n+1}..a_{k-1}W}\overline{\underset{ 2n-k}{\underbrace{PP..P}}}\to P..\underset{2n-k}{\underbrace{PP..P}} {a_{2k-2n+1}..a_{k-1}W}$ and then $n-1$ moves left and go to the previous case. If $a_1=W,a_2n=P$ then by induction hypothesis Tim can transform $a_2..a_{2n-1}$ to $\underset{n-1}{\underbrace{WW..W}}\underset{n-1}{\underbrace{PP..P}}$ using at most $n-1$ moves and now he has one more move for $\underset{n}{\underbrace{WW..W}}\underset{n}{\underbrace{PP..P}}\longrightarrow \underset{n}{\underbrace{PP..P}} \underset{n}{\underbrace{WW..W}}$ If $a_1=W,a_2n=W$ then use the same method as in case 2 to make the first cow purple ( 1 move) and the use the induction hypothesis. Now in order to finish the proof it's enough to show that there is an initial alignment that can't be transormed into $ \underset{n}{\underbrace{PP..P}} \underset{n}{\underbrace{WW..W}}$ using less than $n$ moves. I claim that $WPWP..WP$ works. First condider any alignment $S=a_1a_2..a_{2n}$ and define the number $m(S)=|a_2-a_1|+|a_3-a_2|+...+|a_{2n}-a_{2n-1}|$ where $a_i=1$ if $a_i$ represents a purple cow and $a_i=0$ otherwise. Observe that $m(WPWP..WP)=2n-1$ and $m(\underset{n}{\underbrace{PP..P}} \underset{n}{\underbrace{WW..W}})=1$ Now consider a sequence of moves $S_0\to S_1\to S_2\to....$ I claim that $m(S_{i+1})\geq m(S_i)-2$ Let $S_i=a_1a_2..c_0\underline{c_1c_2..c_t}\overline{b_1b_2..b_t}b_0..\to a_1a_2..c_0{b_1b_2..b_t}{c_1c_2..c_t}b_0...=S_{i+1}$ so only the differences $|c_1-c_0|,|b_1-c_t|,|b_0-b_t|$ are possible change in $m(S_i)$ to $m(S_i+1)$ and thus $m(S_i+1)\geq m(S_1)-3$. Suppose that $m(S_i+1)= m(S_1)-3$, then $|c_1-c_0|=1,|b_1-c_t|=1,|b_0-b_t|=1$ and $|c_0-b_1|=0,|c_1-b_t|=0,|b_0-c_t|=0$ so $c_1\neq c_0=b_1\neq c_t\Rightarrow c_1=c_t$ and $b_0 \neq b_t=c_1=c_t\Rightarrow b_0\neq c_t$ which is impossible since $|b_0-c_t|=0$ So $m(S_i+1)\geq m(S_1)-2$ and since $m(WPWP..WP)-m(PP..PWW..W)=2n-2$ we need at least $(2n-2)/2=n-1$ moves. Suppose that we can do it in $n-1$ moves, then in every move $m(S)$ must be decreased by exactly $2$. Initialy the first cow was White so since at the end is purple there is a move after which the first cow is always white, consider the first such move : ${Wa_2a_3..a_t}{Pb_2b_3..b_t}b_0...\to {Pb_2b_3..b_t}{Wa_2a_3..a_t}b_0...$ and we must have $m({Pb_2b_3..b_t}{Wa_2a_3..a_t}b_0...)=m({Wa_2a_3..a_t}{Pb_2b_3..b_t}b_0...)-2$ so $a_t=0,b_t\neq b_0$ and $b_t=0,a_t=b_0$ but then $a_t=b_0\neq b_t=0=a_t$ a condtradiction. So we need at least $n$ moves and the proof is complete.