아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Subarray Sort

시간 제한1초메모리 제한1024 MB

요약
1부터 N까지의 순열이 주어질 때, 길이 L인 구간을 정렬하는 데 floor(sqrt(L))초가 걸린다면 전체를 정렬하는 최소 시간을 구한다.
난이도

보통10점 중 6점

유형
그리디, 수학, 구현
정답자
아직 제출이 없습니다

문제

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 ⌊j−i+1⌋\lfloor \sqrt{j-i+1} \rfloor seconds, help him find the minimum time he can order all the toys.

제한

  • 1≤N≤4⋅1061 ≤ N ≤ 4 · 10^6
  • ⌊x⌋\lfloor x \rfloor denotes the greatest integer k≤xk ≤ 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 ⌊1−0+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 ⌊3−1+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.

예제2

  1. 예제 1

    입력
    5
    3 1 4 2 5
    
    예상 출력
    2
    
  2. 예제 2

    입력
    3
    1 2 3
    
    예상 출력
    0