Given the number n , the source list ( that is, the source list is sorted and contains elements from 0 to n-1 ):
[0, 1, 2, ... n - 1]
Input is a sequence of numbers m . For each input number, move the number to the top of the list and print the index of that number.
For instance:
n = 5:
Input:
3 3 4 2
Conclusion:
3 0 4 4
Explanation: For n = 5, the initial list
[0, 1, 2, 3, 4]
First move 3 to the front. Index 3 on the list is 3.
[3, 0, 1, 2, 4]
Then we again move 3 forward. Since it is already in front, the index is 0.
[3, 0, 1, 2, 4]
Then we move 4 to the front. Index 4 is 4.
[4, 3, 0, 1, 2]
Finally, we move 2 forward. Index 2 is 4.
[2, 4, 3, 0, 1]
O (mn) , . O (m sqrt (n)).
, , , - , . , ?
user5870009