問題一覧 > 通常問題

No.1425 Yet Another Cyclic Shifts Sorting

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 87
作問者 : 👑 箱星 / テスター : first_vil
お気に入りにしたユーザー ProblemId : 5007 / 自分の提出
問題文最終更新日: 2022-04-25 23:57:52
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 286 (順位表) の他の問題:

Problem Statement

Koboshi received a sequence $A_1,A_2,\ldots,A_N$ of length $N$ as a birthday present. She wants to sort this sequence in ascending order.

A certain store sells $N-1$ types of tools that perform operations on sequences of length $N$. For $i=2,3,\ldots,N$, tool $i$ performs the following operation on a sequence $B_1,B_2,\ldots,B_N$ of length $N$.

  • Perform a cyclic shift on the first $i$ terms. That is, change $B_1,B_2,\ldots,B_i,B_{i+1},\ldots,B_N$ to $B_2,\ldots,B_i,B_1,B_{i+1},\ldots,B_N$.

Koboshi is going to buy $0$ or more types of tools at this store. Each tool she buys can be used any number of times.

What is the minimum number of types of tools needed to sort the sequence $A_1,A_2,\ldots,A_N$ in ascending order?

Input

$N$
$A_1$ $A_2$ $\ldots$ $A_N$
  • All inputs are integers.
  • $2\le N\le 2\times 10^5$
  • $1\le A_i\le 10^9$

Output

Output the minimum number of types of tools needed to sort the sequence in ascending order.

Sample

Sample 1
Input
5
3 4 1 6 7
Output
1

Buy the tool that performs a cyclic shift on the first $3$ terms. Using this tool $1$ time changes the sequence from $3,4,1,6,7$ to $4,1,3,6,7$. Using it $1$ more time changes it to $1,3,4,6,7$, which sorts it in ascending order.

Sample 2
Input
6
5 3 1 2 4 6
Output
2

Performing a cyclic shift on the first $5$ terms changes the sequence to $3,1,2,4,5,6$, and then performing a cyclic shift on the first $3$ terms changes it to $1,2,3,4,5,6$. Thus, the sequence can be sorted in ascending order with $2$ types of tools. It cannot be sorted in ascending order with $1$ or fewer types of tools.

Sample 3
Input
4
1 1 2 3
Output
0

The sequence is already in ascending order from the beginning.

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