問題一覧 > 通常問題

No.365 Jenga Sort

レベル : / AI推定難易度 : 750 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 500
作問者 : 🍡yurahuna / テスター : btk
お気に入りにしたユーザー ProblemId : 1081 / 自分の提出
問題文最終更新日: 2026-06-25 02:25:01
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 136 (順位表) の他の問題:

Problem Statement

$N$ blocks are lined up in a horizontal row. The blocks have lengths $1, 2, ..., N$, but they are in no particular order.
yurahuna has decided to rearrange the blocks so that they are in ascending order of length when viewed from the left.
However, the only operation yurahuna can perform is "choose one block and move it to the leftmost position".
(If the leftmost block is chosen, the arrangement of the blocks does not change after the operation, but this still counts as one operation.)

What is the minimum number of operations yurahuna needs to rearrange the blocks?

Input

$N$
$a_1$ $a_2$ ... $a_N$

  • The first line contains the number of blocks $N$ $(1 \leq N \leq 100000)$.
  • The second line contains the block lengths $a_i$ $(1 \leq i \leq N, 1 \leq a_i \leq N)$ as space-separated integers.
    $a_i$ represents the length of the $i$-th block from the left.
    • All block lengths are distinct. That is, $a_i \neq a_j$ for all $i, j$ $(1 \leq i < j \leq N)$.

Output

Output on one line the minimum number of operations yurahuna needs to rearrange the blocks.
Print a newline at the end.

Sample

Sample 1
Input
5
3 1 4 2 5
Output
2

First choose the block of length 2 and move it to the leftmost position, then choose the block of length 1 and move it to the leftmost position; the blocks are then in ascending order of length when viewed from the left. This is a rearrangement that uses the minimum number of operations.
3 1 4 2 5
↓
2 3 1 4 5
↓
1 2 3 4 5

Sample 2
Input
4
4 3 2 1
Output
3

Choosing and operating on the blocks of length 3, 2, 1 in this order rearranges them in the minimum number of operations.

Sample 3
Input
4
1 2 3 4
Output
0

The blocks are already in ascending order of length when viewed from the left, so yurahuna does not need to perform any operations.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。