問題一覧 > 通常問題

No.1606 Stuffed Animals Keeper

レベル : / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 120
作問者 : SPD_9X2 / テスター : 👑 tute7627 penguinman
お気に入りにしたユーザー ProblemId : 6315 / 自分の提出
問題文最終更新日: 2021-07-14 17:29:59
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 304 (順位表) の他の問題:

Problem Statement

In the room where W lives, $N$ stuffed animals are lined up in a row.

There are $3$ types of stuffed animals: carnivores, herbivores, and birds.

The initial state of the row of stuffed animals is represented by a sequence $A$ of length $N$.

$A_i$ being $0,1,2$ means that the $i$-th stuffed animal from the left in the row is a carnivore, a herbivore, or a bird, respectively.


W calls a row of stuffed animals a safe row of stuffed animals if it satisfies the following condition.

  • No pair of a carnivore and a herbivore is adjacent.


W wants to make the row a safe row of stuffed animals by repeating the following operation any number of times, $0$ or more.

  • Choose $1$ carnivore and one herbivore, and swap their positions.

Determine whether W can obtain a safe row of stuffed animals, and if it is possible, find the minimum number of operations required.

Input

$N$
$A_1\ A_2\ ... A_N$

  • All inputs are integers.
  • $2 \le N \le 5000$
  • $0 \le A_i \le 2$
  • $A_i$ being $0,1,2$ means that the $i$-th stuffed animal from the left in the row is a carnivore, a herbivore, or a bird, respectively.

Output

If the row cannot be made a safe row of stuffed animals no matter how many operations are performed, print $-1$.

Otherwise, print the minimum number of operations required to make it a safe row of stuffed animals.

Print a newline at the end.

Sample

Sample 1
Input
3
2 0 1
Output
-1

No matter how the operations are performed, the row cannot be made a safe row of stuffed animals.

Sample 2
Input
5
0 0 2 2 0
Output
0

There are no herbivores, so the row is a safe row of stuffed animals from the beginning.

Sample 3
Input
15
0 0 1 2 0 1 0 2 0 1 1 2 1 0 1
Output
2

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