eolymp
bolt
Try our new interface for solving problems
Problems

Solitaire

Solitaire

\includegraphics{https://static.e-olymp.com/content/5f/5f78115889cf167819d4ffcd75aa5bc824d03ada.jpg} You have a deck of \textbf{N} cards valued from \textbf{1} to \textbf{N}. The game starts with cards facing down in the "initial" location. There are also three other locations where you can play your cards face up (once they are face up at the top of any of the other piles): "goal", "helper" and "pile". You win the game once all the cards are placed on the goal in ascending order (\textbf{N} on the top). Rules: \begin{enumerate} \item you can play cards onto "goal" only if the top "goal" card's value is one less than the value of the card you are trying to place (if "goal" is empty, you can only play the card with value \textbf{1 }(one) there). E.g. if the top card in "goal" is \textbf{3}, you can only play \textbf{4} to "goal". \item you can play cards onto "helper" only if the top "helper" card's value is one more than the value of the card you are trying to place (if "helper" is empty, you can only play the card with value \textbf{N }there). E.g. if the top card in "helper" is \textbf{8}, you can only play \textbf{7} to "helper". \item you can only move a card onto the "pile" from the top of the "initial" deck by turning that card over(remember? cards in the initial deck are facing down). \item once all cards are facing up ("initial" deck is empty) and if the game is not finished yet, take the cards from the "pile" and turn all of them over onto the "initial" position. This will be your new "initial deck". To clarify - the top card in "pile" (facing up) will become the bottom card in "initial" (facing down). \end{enumerate} What is the minimum number of type \textbf{4} moves you need to finish the game? \InputFile First line of the input contains an integer \textbf{T} (\textbf{1} ≤ \textbf{T} ≤ \textbf{100}) - the number of test cases. Each test case consists of two lines: First line contains an integer \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{1000}) Second line contains description of the "initial" deck. The first number is the value of the card at the bottom of the initial deck facing down (so the last card will be played first onto "pile"). This will be a permutation of the list of integers from \textbf{1} to \textbf{N}. \OutputFile For each test case print the minimum number of type \textbf{4} moves you neeed to "win" the game on a separate line.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
3
1 2 3
6
6 2 4 3 5 1
Output example #1
0
1
Author Darko Aleksic
Source ACM ICPC CCPC 2013