Logo
(追記) (追記ここまで)

34612번 - Book Sorting 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB333100.000%

문제

You have $n$ books arranged from left to right on a bookshelf. These books are uniquely labeled from 1ドル$ to $n$. The $i$-th book from the left is labeled $p_i$. You want to sort the books so that their labels are in ascending order from left to right.

In one step, you can perform one of the following actions:

  • Choose two adjacent books and swap them.
  • Choose one book and move it to the leftmost position.
  • Choose one book and move it to the rightmost position.

Compute the minimum number of steps required to sort the books.

입력

The first line of input contains an integer $n$ (2ドル ≤ n ≤ 500,円 000$). The second line contains $n$ pairwise distinct integers $p_1, p_2, \dots , p_n$ (1ドル ≤ p_i ≤ n$).

출력

Output the minimum number of steps to sort the books in ascending order from left to right by their labels.

제한

예제 입력 1

6
6 2 1 4 3 5

예제 출력 1

3

You can do the following three steps in order: swap the books labeled 2ドル$ and 1ドル,ドル swap the books labeled 4ドル$ and 3ドル,ドル and move the book labeled 6ドル$ to the rightmost position.

6 2 1 4 3 56 1 2 4 3 56 1 2 3 4 51 2 3 4 5 6

It can be shown that two or fewer steps are insufficient to sort the books.

예제 입력 2

9
9 2 4 3 7 5 1 8 6

예제 출력 2

5

노트

출처

ICPC > Regionals > Asia Pacific > Asia Pacific Championship > The 2025 ICPC Asia Pacific Championship K번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /