Problem Statement
Bear Limak was chilling in the forest when he suddenly found a computer program. The program was a correct implementation of MergeSort. Below you can find the program in pseudocode.
# mergeSort(left,right) sorts elements left, left+1, ..., right-1 of a global array T function mergeSort(left,right): # if there is at most one element, we are done if left+1 >= right: return # otherwise, split the sequence into halves, sort each half separately mid = (left + right) div 2 mergeSort(left,mid) mergeSort(mid,right) # then merge the two halves together merged = [] #Â an empty sequence p1 = left p2 = mid while (p1 < mid) or (p2 < right): if p1 == mid: merged.append( T[p2] ) p2 += 1 else if p2 == right: merged.append( T[p1] ) p1 += 1 else: if LESS( T[p1], T[p2] ): merged.append( T[p1] ) p1 += 1 else: merged.append( T[p2] ) p2 += 1 # finally, move the merged elements back into the original array for i from left to right-1 inclusive: T[i] = merged[i-left]
Limak noticed that one part of the implementation was missing: the function LESS. You can probably guess that the function is supposed to return a boolean value stating whether the first argument is less than the second argument. However, Limak is a bear and he didn't know that. Instead he implemented his own version of this function. Limak's function uses a true random number generator. Each of the two possible return values (true, false) is returned with probability 50 percent.
The random values generated in different calls to Limak's function LESS are mutually independent. Note that even if you call LESS twice with the same arguments, the two return values may differ.
After implementing LESS, Limak decided to run his brand new program. He initialized the global array T to contain N elements. Then, he filled the values 1 through N into the array: for each valid i, he set T[i] to i+1. Finally, he executed the function mergeSort(0,N).
Even with Limak's new LESS function, the program never crashes. On the other hand, it does not necessarily produce a sorted sequence when it terminates. In general, when the program terminates, the array T will contain some permutation of the numbers 1 through N.
After running the program many times, Limak has noticed that different output permutations are produced with different probabilities. Your task is to help him learn more about these probabilities.
Imagine a list of all N! permutations of numbers 1 through N. For each such permutation P we have computed the probability prob(P) that Limak's program outputs P. We will now order all those N! permutations according to that probability in descending order. (I.e., the most probable permutations are at the beginning of the list.) Ties are broken using lexicographic order. (I.e., if two permutations have exactly the same probability, the lexicographically smaller one is sooner in the list.)
You are given a
Definition
- Class:
- BearSorts
- Method:
- index
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int index(int[] sortedSequence)
- (be sure your method is public)
Notes
- Given two different permutations of 1 through N, the lexicographically smaller one is the one with the smaller element at the first index where they differ.
Constraints
- sortedSequence will contain exactly N elements.
- N will be between 1 and 40, inclusive.
- Elements of sortedSequence will be between 1 and N, inclusive.
- Elements of sortedSequence will be pairwise distinct.
Examples
{1,3,2}
Returns: 2
Some of the 3! = 6 possible permutations are more likely than others. However, in this specific case it just happens that the correct sorted order of permutations is the same as their lexicographic order. Hence, the 1-based index of the permutation {1,3,2} is 2.
{1,3,2,4}
Returns: 9
For N = 4, the correct sorted order of permutations is not the same as their lexicographic order. The given permutation {1,3,2,4} is third in lexicographic order but only ninth in the order according to their probability. More precisely: There are 8 permutations such that each of them will appear with a strictly greater probability. There are some other permutations that have the same probability as {1,3,2,4}, but all of these are lexicographically greater than {1,3,2,4}.
{5,4,3,2,1}
Returns: 72
{1,2,4,3,5,6,7}
Returns: 33
{40,39,38,37,36,35,20,33,32,31,30,29,2,27,26,17,24,23,22,21,19,34,18,6,3,15,14,13,12,11,10,9,8,7,25,5,4,16,28,1}
Returns: 984023422
{1}
Returns: 1
{1,2}
Returns: 1
{2,1}
Returns: 2
{1,2,3,4,5,6,7}
Returns: 1
{6,2,4,5,8,1,7,3}
Returns: 24734
{9,8,7,1,5,4,6,2,3}
Returns: 201383
{14,4,5,9,3,6,7,8,10,13,11,16,2,1,15,12}
Returns: 527738568
{6,7,3,4,5,13,2,8,9,10,11,17,14,1,15,16,12}
Returns: 239321519
{9,19,1,2,8,3,13,18,17,12,16,14,10,11,20,7,21,15,4,6,5}
Returns: 403747104
{23,22,19,18,17,13,21,16,5,4,8,20,9,10,11,12,7,6,15,14,3,2,1}
Returns: 295653159
{7,16,29,19,11,8,6,12,14,21,2,28,10,15,13,4,26,22,25,30,32,27,5,3,9,23,17,20,24,18,1,31}
Returns: 327917827
{1,2,3,4,5,6,7,8,15,10,11,12,13,14,9,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,34,32,33,31,35}
Returns: 494925550
{37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}
Returns: 888692065
{40,28,24,37,36,19,34,32,33,31,22,29,39,27,20,13,1,10,30,21,8,15,18,17,16,35,14,25,12,11,23,9,26,7,6,5,4,3,2,38}
Returns: 373383976
{27,10,8,5,31,13,29,1,30,32,36,24,39,11,21,22,3,4,15,12,20,2,33,6,37,38,16,28,18,9,25,34,23,19,14,7,35,17,26}
Returns: 298490132
{1,26,17,19,5,4,39,8,9,10,11,31,13,14,15,16,3,18,24,20,12,22,23,6,36,2,27,28,29,30,33,32,21,34,7,25,37,38,35,40}
Returns: 426919515
{7,37,31,1,21,24,32,36,25,26,28,33,34,17,29,6,3,19,20,27,14,4,16,15,18,35,12,23,10,9,8,38,11,5,30,22,2,13}
Returns: 194870697