問題一覧 > 通常問題

No.1053 Gaming Stick

レベル : / AI推定難易度 : 900 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 215
作問者 : QCFium / テスター : tko919
お気に入りにしたユーザー ProblemId : 4228 / 自分の提出
問題文最終更新日: 2020-05-15 21:47:05
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 248 (順位表) の他の問題:

Problem Statement

You found a colorful stick lying on the roadside. This stick, lying horizontally in the left-right direction, is sufficiently thin and is divided into $N$ parts, and the $i$-th part from the left is painted in color $A_i$.
It is colorful as it is, but to make it more beautiful, you want it to satisfy the following: "each color, if it appears, appears entirely within one contiguous segment".
To this end, you perform the following operation $0$ or more times.

  • Cut the stick at one place. Swap the positions of the 2 resulting sticks and join them. (Neither stick is reversed.)
Can the condition be satisfied? If so, what is the minimum number of operations needed to satisfy it?
A constraint violation was discovered in the test data, so it was corrected and a rejudge was performed. We apologize. (2020/05/15 21:47)

Input

$N$
$A_1\ A_2\ A_3\ \dots\ A_N$

$1 \le N \le 10^5$
$1 \le A_i \le N$
All inputs are integers.

Output

If it is impossible to satisfy the condition, print -1.
Otherwise, print the number of operations required to satisfy the condition.

Sample

Sample 1
Input
3
1 2 1
Output
1

For example, by cutting at the right end of the leftmost section and performing the operation, the colors of the stick after the operation become $2,1,1$ from left to right, which satisfies the condition.

Sample 2
Input
3
1 2 3
Output
0

The condition is already satisfied as it is.

Sample 3
Input
5
1 2 1 2 1
Output
-1

No matter what you do, the condition cannot be satisfied.

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