Subarray Sort

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

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.

제한

  • 1N41061 ≤ N ≤ 4 · 10^6
  • x\lfloor x \rfloor denotes the greatest integer kxk ≤ 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.

힌트

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 \dots \rfloor = 1 second. The permutation becomes 11 33 44 22 55. 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 \dots \rfloor = 1 second. The permutation becomes 11 22 33 44 55. In total Little Triangle can sort all the toys in 1+1=21 + 1 = 2 seconds, which is also the minimum possible time.

In the second example, the toys are already sorted.