A Fair shuffle is a specific method of shuffling $$$N$$$ distinct cards arranged horizontally from left to right. In a Fair shuffle, the cards are divided into two contiguous partitions which may have different card counts, and one of them may even have zero cards. Let's denote the left partition as $$$L$$$ and the right partition as $$$R$$$.
Cards from the left partition ($$$L$$$) are then merged with the cards from the right partition ($$$R$$$) while maintaining the relative order between the cards of each partition.
Given the final permutation obtained after applying $$$K$$$ Fair shuffles to an initial sequence of $$$N$$$ non-repeating cards numbered from $$$1$$$ to $$$N$$$, your task is to determine the minimum possible value of $$$K$$$.
For example, if we start with the sequence 1 2 3 4 5 and perform a Fair shuffle by partitioning it into $$$L$$$: 1 2 and $$$R$$$: 3 4 5, cards from $$$L$$$ and $$$R$$$ can be merged in various ways:
Assume that the outcome of the first Fair shuffle is 1 3 2 4 5. If we perform a second Fair shuffle on it, we could partition the sequence into $$$L$$$: 1 3 2 4 and $$$R$$$: 5 and merge it as 1 3 2 5 4.
The first line contains an integer $$$N (1 \leq N \leq 10^6)$$$, the number of cards in the deck. The second line contains a permutation of integer numbers from $$$1$$$ to $$$N$$$ describing the final arrangement of the cards.
Output a single line with an integer $$$K$$$ indicating the minimum number of Fair shuffles required to transform the initial sequence into the given one.
5 3 4 5 1 2
1
10 1 6 5 2 10 3 4 8 7 9
3
5 5 4 2 3 1
2
8 1 6 2 3 7 8 4 5
1
5 1 2 3 4 5
0
Name |
---|