QOJ.ac

QOJ

시간 제한: 1 s 메모리 제한: 64 MB 총점: 20

#23773. 碁石ならべ

통계

問題

白と黒の碁石をテーブルの上にならべて遊ぶ.まずテーブルの左端に碁石を置く.次に左から $2$ 番目の場所に碁石を置く.これを $n$ 回繰り返して $n$ 個の碁石を横一列にならべる.ただし,新しく $i$ 番目の碁石を置く際には,次のルールに従ってテーブル上の碁石を置き換える.

  • $i$ が奇数の場合:テーブルに置いてあった碁石は置き換えず,新しい碁石を左から $i$ 番目に置く.
  • $i$ が偶数の場合:新しく左から $i$ 番目に置く碁石の色とテーブル上の右端の碁石の色が同じ場合は,テーブル上の碁石は置き換えず,新しい碁石を左から $i$ 番目に置く.そうでない場合,すなわち,新しく左から $i$ 番目に置く碁石の色とテーブル上の右端の碁石の色が異なる場合は,まずテーブル上の右端の連続した同色の碁石を全て取り除き,$i$ 番目の碁石と同色の碁石に置き換える.そしてテーブルの右端に $i$ 番目の碁石を置く.

例えば,最初の $7$ 個の碁石を置いた時点で,

$$\circ \circ \bullet \bullet \circ \circ \circ$$

となっていたとする($\circ$ は白の碁石を,$\bullet$ は黒の碁石を表す.)

  • $8$ 番目の碁石が白($\circ$)の場合は,右端の碁石と同色なので,そのまま置く.したがって,テーブル上の碁石は

$$\circ \circ \bullet \bullet \circ \circ \circ \circ$$

  • $8$ 番目の碁石が黒($\bullet$)の場合は,右端の碁石($\circ$)と色が異なるので,まずテーブルの右端にある $3$ 個の連続した白い碁石($\circ$)を取り除き,黒い碁石($\bullet$)に置き換える.そして右端に $8$ 番目の碁石を置く.したがって,テーブル上の碁石は

$$\circ \circ \bullet \bullet \bullet \bullet \bullet \bullet$$

入力として置く碁石の順番が与えられたとき,$n$ 個の碁石をならべ終わった後,テーブル上に置いてある白い碁石の個数を求めるプログラムを作成せよ.

入力

$1$ 行目には正整数 $n$ ($1 \le n \le 100\,000$) が書かれている.$2$ 行目以降の第 $i+1$ 行目 ($1 \le i \le n$) には,$i$ 番目に置く碁石の色を表す整数 $c_{i}$ が書かれており,$c_{i}$ が $0$ なら $i$ 番目に置く碁石の色が白であることを,$1$ なら $i$ 番目に置く碁石の色が黒であることを表す.

採点用データのうち,配点の $50\%$ 分については,$n \le 10\,000$ を満たす.

出力

$n$ 個の碁石をならべ終わった後にテーブル上に置いてある白い碁石の個数だけを含む $1$ 行を出力せよ.

入出力の例

入力例 1

8
1
0
1
1
0
0
0
0

出力例 1

6

入力例 2

8
1
0
1
1
0
0
0
1

出力例 2

2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.