Subarray Sort

Time limit: 0.5s Memory limit: 64MB Input: Output:

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 11 to NN. In order for Little Square not to be upset with him, Little Triangle has to put his toys back in order: 1,2,,N1, 2, \dots, N.

Initially, all the toys are lined up on a shelf in some order.

Knowing that Little Triangle can sort a continuous interval [i,j][i, j] of toys in ji+1\lfloor \sqrt{j - i + 1} \rfloor 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 NN, the number of toys, and PP, an array (indexed from 00) 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 NN
  • on the second line, the permutation PP

The sample grader prints the result returned by solve to the standard output.

Attention! The contestant should not implement the main function.

Constraints

  • 1N41061 \leq N \leq 4 \cdot 10^6
  • x\lfloor x \rfloor denotes the greatest integer kxk \leq x
  • Each number between 11 and NN will appear exactly once in PP.
  • The grader given to the contestants is not necessarily the same with the grader used for scoring.
# Points Constraints
1 7 PP is randomly generated.
2 8 1N91 \leq N \leq 9
3 35 1N2 0001 \leq N \leq 2 \ 000
4 25 1N100 0001 \leq N \leq 100 \ 000
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 [0,1][0, 1] in 10+1=2=1.41421...=1\lfloor \sqrt{1 - 0 + 1} \rfloor = \lfloor \sqrt{2} \rfloor = \lfloor 1.41421... \rfloor = 1 second. The permutation becomes 1 3 4 2 51 \ 3 \ 4 \ 2 \ 5. He can now sort the interval [1,3][1, 3] in 31+1=3=1.73205...=1\lfloor \sqrt{3 - 1 + 1} \rfloor = \lfloor \sqrt{3} \rfloor = \lfloor 1.73205... \rfloor = 1 second. The permutation becomes 1 2 3 4 51 \ 2 \ 3 \ 4 \ 5. In total Little Triangle can sort all the toys in 1+1=21 + 1 = 2 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.

Log in or sign up to be able to send submissions!