Talk:LA - cNteSahruPfefrlefe

We didn't solve this problem at world finals, but Steve and John worked on it for a while and the three of us came up with some insights. First off, it's easy to calculate the number of shuffles in the input. For each possible number of shuffles (1 &le; n &le; 10), there can be at most 2n deviations in the input when compared to the canonical ordering (shuffling the sorted deck n times without making any mistakes). Exactly one value of n will match these criteria, since the canonical orderings are so different for each number of shuffles.

Once you know the number of shuffles, you have to figure out the locations of the mistakes. The best strategy is to work backward from the input, "unshuffling" the cards at each step (possibly making a single mistake in the process) until the deck returns to sorted order. I recommend using an A* search, where the search heuristic is the number of mistakes left to fix. We can estimate this value by dividing the number of deviations against the canonical ordering by two. --Jeff 14:11, 9 Sep 2005 (EDT)

Based on Jeff's analysis, I used the following approach to get a backtracking solution that seem to solve all valid cases I was able to create.

Recall that a Hamming distance H(X,Y) between two vectors X and Y is the number of different corresponding elements in them. Denote as s(X) shuffling vector X.

Denote the initial vector as X. After we have found the number of shuffles n, let X_0, X_1, etc, X_n be the deck after the corresponding number of shuffles and mistakes, and S_0, S_1, etc, S_n --- the deck after corresponding number of shuffles, if no mistakes were made. Note that X = X_n, and S_0 = X_0.

We already know that n is the number between 1 and 10, such that H(S_n, X) is minimal possible. We now use similar way to restore the possible series of mistakes. Create the sequence Y_n, Y_{n-1}, etc, Y_0 by reverse shuffling the vector X_n. Here Y_n = X_n, Y_{n-1}=s^{-1}(X_n), etc. Fix the number of errors (we need to minimize it, remember). Suppose we have restored all the errors in the first i-1 shuffles, there are l errors left, and would like to choose whether we must make a error after the i-th shuffle, and if we do, where will we make it. We have vector X_{i-1}. Create X_i'=s(X_{i-1}). If H(X_i',Y_i) <= 2*l, we can continue without a error (we make a backtracking call, choosing X_i = X_i'). Also we try all possible positions for the next error, creating all valid vectors X_i. We continue backtracking for those of them, that have H(X_i, Y_i) <= 2*(l - 1).

Final note - there is a condition that the optimal solution is unique. I did not try to use this fact, but it is indeed not necessarily true. For example, test

24 49 21 46 45 43 15 40 12 37 9 34 6 31 3 28 0 25 50 22 47 19 44 16 41 13 38 10   35 7 32 29 4 51 26 11 23 48 20 18 17 42 14 39 1 30 8 33 5 36 2 27

has several possible solutions, lexicographically first (no error is smaller than any error position) is:

Number of shuffles = 10 Error in shuffle 1 at location 37 Error in shuffle 4 at location 31 Error in shuffle 8 at location 37 Error in shuffle 9 at location 16 Error in shuffle 10 at location 31

lexicographically last is:

Number of shuffles = 10 Error in shuffle 1 at location 37 Error in shuffle 2 at location 47 Error in shuffle 4 at location 31 Error in shuffle 8 at location 37 Error in shuffle 10 at location 31

--Andrew Stankevich