After spending winter break at his grandpa’s, Little Square returned home. While he was away, his friend, Little Triangle, played with his toys, numbered from to . In order for Little Square not to be upset with him, Little Triangle has to put his toys back in order: .
Initially, all the toys are lined up on a shelf in some order.
Knowing that Little Triangle can sort a continuous interval of toys in seconds, help him find the minimum time he can order all the toys.
Interaction protocol
The contestant must implement one function with the following signature:
int solve(int N, int P[]);
solve
will be called exactly once.
The function will be supplied with , the number of toys, and , an array (indexed from ) containing the initial order of the toys. It has to return an int
representing the minimum time required by Little Triangle to sort all the toys.
The sample grader reads the input from standard input with the following format:
- on the first line, the number
- on the second line, the permutation
The sample grader prints the result returned by solve
to the standard output.
Attention! The contestant should not implement the main
function.
Constraints
- denotes the greatest integer
- Each number between and will appear exactly once in .
- The grader given to the contestants is not necessarily the same with the grader used for scoring.
# | Points | Constraints |
---|---|---|
1 | 7 | is randomly generated. |
2 | 8 | |
3 | 35 | |
4 | 25 | |
5 | 25 | No additional constraints. |
Example 1
stdin
5
3 1 4 2 5
stdout
2
Explanation
In the first example, Little Triangle can sort the interval in second. The permutation becomes . He can now sort the interval in second. The permutation becomes . In total Little Triangle can sort all the toys in seconds, which is also the minimum possible time.
Example 2
stdin
3
1 2 3
stdout
0
Explanation
In the second example, the toys are already sorted.