Problem:


 碁石ならべ 

問題

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

  
 i が奇数の場合: テーブルに置いてあった碁石は置き換えず,新しい碁石を左
    から i 番目に置く.

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

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

   ○○●●○○○

となっていたとする.   (○は白の碁石を,●は黒の碁石を表す. )

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

      
○○●●○○○○

    となる.
  

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

      ○○●●●●●●

    となる.

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

入力

入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．

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

  採点用データのうち, 配点の 50% 分については, n ≤ 10000 を満たす.

n が 0 のとき入力の終了を示す. データセットの数は 10 を超えない．

出力

 データセットごとに, n 個の碁石をならべ終わった後にテーブル上に置いてある白い碁石の個数を 1 行に出力する.

入出力例

入力例

8
1
0
1
1
0
0
0
0
8
1
0
1
1
0
0
0
1
0

出力例

6
2

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
8
1
0
1
1
0
0
0
0
8
1
0
1
1
0
0
0
1
0


Sample output:
6
2

Submission status: Wrong Answer

Solution code:
while True:
    n=int(input())
    if n==0:break
    com=[int(input())for i in range(n)]
    seq=1
    total=0
    isW=com[0]==0
    for i in range(1,n):
        if i%2!=0:
            seq+=1
            isW=com[i]==0
        else:
            if isW:
                if com[i]==0:seq+=1
                else:
                    total+=seq
                    seq=1
                    isW=False
            else:
                if com[i]==0:
                    seq=1
                    isW=True
                else:seq+=1
    if isW:total+=seq
    print(total)

Submission status: Wrong Answer

Solution code:
while True:
    n=int(input())
    if n==0:break
    com=[int(input())for i in range(n)]
    seq=[1]
    isW=com[0]==0
    stW=isW
    for i in range(1,n):
        if i%2!=0:
            if len(seq)!=1:
                seq[len(seq)-2]+=seq.pop()+1
            else:
                seq[0]+=1
                stW=not stW
            isW=com[i]==0
        else:
            if isW:
                if com[i]==0:seq[len(seq)-1]+=1
                else:
                    seq.append(1)
                    isW=False
            else:
                if com[i]==0:
                    seq.append(1)
                    isW=True
                else:seq[len(seq)-1]+=1
    total=0
    if stW:total+=sum(seq[0::2])
    else:total+=sum(seq[1::2])
    print(total)

Submission status: Wrong Answer

Solution code:
import re
n = int(raw_input())

white = re.compile('(1*)$')
black = re.compile('(0*)$')

s = ''
for i in xrange(n):
    if (i + 1) % 2 != 0:
        s += raw_input()

    else:
        p = raw_input()
        if p == '0':
            m = white.search(s)
            s = white.sub('0' * len(m.group(1)), s)
            s += '0'
        else:
            m = black.search(s)
            s = black.sub('1' * len(m.group(1)), s)
            s += '1'
print s.count('0')

Submission status: Wrong Answer

Solution code:
while True:

    n = int(raw_input())

    if n == 0:
        break

    li = []

    for i in xrange(n):
        p = int(raw_input())
        if i == 0:
            li.append([p, 1])
            continue
        # odd
        if i % 2 == 0:
            if li[-1][0] == p:
                li[-1][1] += 1
            else:
                li.append([p, 1])
        else:
            if li[-1][0] == p:
                li[-1][1] += 1
            else:
                li[-1][0] = p
                li[-1][1] += 1


    print sum(t[1] for t in li if t[0] == 0)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())


li = []

for i in xrange(n):
    p = int(raw_input())
    if i == 0:
        li.append([p, 1])
        continue
    # odd
    if i % 2 == 0:
        if li[-1][0] == p:
            li[-1][1] += 1
        else:
            li.append([p, 1])
    else:
        if li[-1][0] == p:
            li[-1][1] += 1
        else:
            li[-1][0] = p
            li[-1][1] += 1
            if len(li) >= 2 and li[-2][0] == li[-1][0]:
                li[-2][1] += li[-1][1]
                del li[-1]

    print repr(li)

print sum(t[1] for t in li if t[0] == 0)

Submission status: Time Limit Exceeded

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    arr = [0 for i in range(n)]
    for i in range(n):
        stone = int(input())
        if i%2 == 0:
            arr[i] = stone
        else:
            for j in reversed(range(i)):
                if arr[j] != stone:
                    arr[j] = stone
                else:
                    break
            arr[i] = stone
    print(arr.count(0))

Submission status: Time Limit Exceeded

Solution code:
while True:
    n = int(input())
    if n == 0: break
    line = []
    for i in range(n):
        s = input()
        if i % 2 == 0:
            line.append(s)
        else:
            if line[-1] == s:
                line.append(s)
            else:
                k = 0
                for i in range(len(line))[::-1]:
                    if line[i] == s:
                        k = i
                        break
                m = len(line[k:])
                del line[k:]
                line = line + [s]*m + [s]
    print(sum([1 for l in line if l == '0']))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
while True:
    n = int(input())
    if n == 0: break
    line = deque()
    for i in range(n):
        s = input()
        if i % 2 == 0:
            line.append(s)
        else:
            if line[-1] == s:
                line.append(s)
            else:
                j = 0
                for i in range(len(line))[::-1]:
                    j += 1
                    if line[i] == s:
                        break
                for _ in range(j): line.pop()
                for _ in range(j+1): line.append(s)
    print(sum([1 for l in line if l == '0']))

Submission status: Time Limit Exceeded

Solution code:
import re

white = re.compile('(1*)$')
black = re.compile('(0*)$')

while True:

    n = int(raw_input())

    if n == 0:
        break

    s = ''
    for i in xrange(n):
        if (i + 1) % 2 != 0:
            s += raw_input()

        else:
            p = raw_input()
            if p == '0':
                m = white.search(s)
                s = white.sub('0' * len(m.group(1)), s)
                s += '0'
            else:
                m = black.search(s)
                s = black.sub('1' * len(m.group(1)), s)
                s += '1'
    print s.count('0')

Submission status: Time Limit Exceeded

Solution code:
while True:
    n=input()
    if n==0:break
    table=[]
    for i in range(1,n+1):
        c=input()
        if i%2:
            table.append(c)
        else:
            if table[-1]==c:
                table.append(c)
            else:
                j=len(table)-1
                while table[j]!=c and j>=0:
                    table[j]=c
                    j-=1
                table.append(c)
    print table.count(0)

Submission status: Accepted

Solution code:
while (1):
    n=int(raw_input())
    if n==0:
        break
    else:
        top=-1
        fault=[0]
        S=0
        for i in range(n/2): #i=0,2,... is othello i=1,3,...is donothing
            s=int(raw_input()) #oddstone
            if top!=s: 
                fault.append(i)
                top=s
            s=int(raw_input()) #evenstone
            if top!=s:
                fault.pop()
                top=s
            if fault==[]:
                fault=[0]
        i=len(fault)-1
        while i>0:
            S=S+(fault[i]-fault[i-1])*2
            i=i-2
        if not top:
            S=2*(n/2)-S
        if n%2==1:
            s=int(raw_input())
            S=S+(1-s)
        print S

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n == 0: break
    l = [0,0]
    l += [input()]
    l += [1]
    for i in range(n-1):
        x = input()
        if i % 2 == 1:
            if l[-2] == x: l[-1] += 1
            else: l += [x]; l += [1]
        else:
            if l[-2] == x: l[-1] += 1
            else:
                if len(l) > 2:
                    l[-3] += l[-1] + 1
                    l = l[:-2]
                else:
                    l[-2] = x
                    l[-1] += 1
    ans = 0
    for i in range(len(l)/2):
        if l[i*2] == 0: ans += l[i*2 + 1]
    print ans

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n == 0: break
    l = [input(),1]
    for i in range(n-1):
        x = input()
        if i % 2 == 1:
            if l[-2] == x: l[-1] += 1
            else: l += [x]; l += [1]
        else:
            if l[-2] == x: l[-1] += 1
            else:
                if len(l) > 2:
                    l[-3] += l[-1] + 1
                    l = l[:-2]
                else:
                    l[-2] = x
                    l[-1] += 1
    ans = 0
    for i in range(len(l)/2):
        if l[i*2] == 0: ans += l[i*2 + 1]
    print ans

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n == 0: break
    l = [input(),1]
    for i in range(n-1):
        x = input()
        if i % 2 == 1:
            if l[-2] == x: l[-1] += 1
            else: l += [x]; l += [1]
        else:
            if l[-2] == x: l[-1] += 1
            else:
                if len(l) > 2:
                    l[-3] += l[-1] + 1
                    l = l[:-2]
                else:
                    l[-2] = x
                    l[-1] += 1
    print sum([l[2*i+1] for i in range(len(l)/2) if l[2*i] == 0])

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a=[int(input()),1]
    for i in range(n-1):
        b=int(input())
        if a[-2]==b:a[-1]+=1
        if i&1 and a[-2]!=b:a+=[b]+[1]
        elif a[-2]!=b:
            if len(a)>2:
                a[-3]+=a[-1]+1
                a=a[:-2]
            else:
                a[-2]=b
                a[-1]+=1
    print(sum(a[2*i+1] for i,x in enumerate(a[::2])if x==0))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n==0: break
    edge = 0
    sq = 0
    past = []

    for i in range(n):
        nxt = int(input())
        if i==0:
            edge = nxt

        if i%2 == 0:
            if nxt == edge:
                sq += 1
            else:
                edge = nxt
                past.append(sq)
                sq = 1
        else:
            if nxt == edge:
                sq += 1
            else:
                edge = nxt
                sq += 1
                if len(past) != 0:
                    sq += past.pop()
    past.append(sq)

    answer = 0
    for i in range(len(past)):
        if edge == 0:
            answer += past[-i-1]
        edge = 1 - edge

    print(answer)

Submission status: Accepted

Solution code:
while True:
    n=int(input())
    if n==0:break
    com=[int(input())for i in range(n)]
    seq=[1]
    isW=com[0]==0
    stW=isW
    for i in range(1,n):
        if i%2!=0:
            if len(seq)!=1:
                if (isW and com[i]==1)or(not isW and com[i]==0):
                    seq[len(seq)-2]+=seq.pop()+1
                else:seq[len(seq)-1]+=1
            else:
                seq[0]+=1
                if (isW and com[i]==1)or(not isW and com[i]==0):stW=not stW
            isW=com[i]==0
        else:
            if isW:
                if com[i]==0:seq[len(seq)-1]+=1
                else:
                    seq.append(1)
                    isW=False
            else:
                if com[i]==0:
                    seq.append(1)
                    isW=True
                else:seq[len(seq)-1]+=1
    total=0
    if stW:total=sum(seq[0::2])
    else:total=sum(seq[1::2])
    print(total)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if not n: break
  count = 0
  flag = -1
  lst = [0]
  for i in range(n):
    a = int(input())
    if flag == a:
      lst[-1] += 1
    else:
      if i % 2 == 0:
        lst.append(1)
        flag = a
      else:
        if len(lst) == 1:
          lst[-1] += 1
          flag = a
        elif len(lst) >= 2:
          lst.append(lst.pop() + lst.pop() + 1)
          flag = a
  ans = 0
  if flag == 0:
    for i in range(-1,len(lst) * (-1) - 1,-2):
      ans += lst[i]
  elif flag == 1:
    for i in range(-2,len(lst) * (-1) - 1,-2):
      ans += lst[i]
  print(ans)

Submission status: Accepted

Solution code:
def main():
  while True:
    n = int(input())
    if not n: break
    flag = -1
    lst = []
    for i in range(n):
      a = int(input())
      if flag == a:
        lst[-1] += 1
      else:
        if i % 2 == 0:
          lst.append(1)
          flag = a
        else:
          if len(lst) == 1:
            lst[-1] += 1
            flag = a
          elif len(lst) >= 2:
            lst[-2] = lst[-2] + lst[-1] + 1
            lst.pop()
            flag = a
    ans = 0
    if flag == 0:
      for i in range(-1,len(lst) * (-1) - 1,-2):
        ans += lst[i]
    elif flag == 1:
      for i in range(-2,len(lst) * (-1) - 1,-2):
        ans += lst[i]
    print(ans)

main()

Submission status: Accepted

Solution code:
while 1:
    N = int(input())
    if N == 0:
        break
    st = []
    last = None
    for i in range(N):
        s = int(input())
        if last is None:
            last = [1, s]
            st.append(last)
            continue
        if last[1] == s:
            last[0] += 1
        else:
            if i % 2:
                P = st.pop()
                if st:
                    last = st[-1]
                    last[0] += P[0] + 1
                else:
                    last = [P[0]+1, s]
                    st.append(last)
            else:
                last = [1, s]
                st.append(last)
    ans = 0
    for s, c in st:
        if c == 0:
            ans += s
    print(ans)

Submission status: Accepted

Solution code:
# AOJ 0527: Setting Go Stones
# Python3 2018.7.1 bal4u

while True:
	n = int(input())
	if n == 0: break
	a, c = [0]*100005, [0]*100005
	sz, a[0], c[0] = 0, 1, int(input())
	for i in range(n-1):
		color = int(input())
		if c[sz] == color: a[sz] += 1
		elif i & 1:
			sz += 1
			a[sz] = 1
		else:
			if sz == 0: a[sz] += 1
			else:
				a[sz-1] += a[sz]+1
				sz -= 1
		c[sz] = color;
	ans = 0
	for i in range(sz+1):
		if c[i] == 0: ans += a[i]
	print(ans)

Submission status: Accepted

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    li = []

    for i in xrange(n):
        p = int(raw_input())
        if i == 0:
            li.append([p, 1])
            continue
        # odd
        if i % 2 == 0:
            if li[-1][0] == p:
                li[-1][1] += 1
            else:
                li.append([p, 1])
        else:
            if li[-1][0] == p:
                li[-1][1] += 1
            else:
                li[-1][0] = p
                li[-1][1] += 1
                if len(li) >= 2 and li[-2][0] == li[-1][0]:
                    li[-2][1] += li[-1][1]
                    del li[-1]

    print sum(t[1] for t in li if t[0] == 0)

Submission status: Accepted

Solution code:

while True:
  stones = [0, 0]
  n = input()
  if not n:
    break
  stones.append(input())
  stones.append(1)
  for i in range(n - 1):
    m = input()
    if i % 2 == 1:
      if stones[-2] == m:
        stones[-1] += 1
      else:
        stones.append(m)
        stones.append(1)
    else:
      if stones[-2] == m:
        stones[-1] += 1
      else:
        if len(stones) > 2:
          stones[-3] += stones[-1] + 1
          stones = stones[:-2]
        else:
          stones[-2] = m
          stones[-1] += 1
  ans = 0
  for j in range(0, len(stones), 2):
    if stones[j] == 0:
      ans += stones[j + 1]
  print ans

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)


class Info:
    def __init__(self,arg_color,arg_length):
        self.color = arg_color
        self.length = arg_length


while True:
    N = int(input())
    if N == 0:
        break

    S = []

    tmp = int(input())
    S.append(Info(tmp,1)) #最初のデータ

    for i in range(1,N):
        tmp = int(input())

        if i%2 == 0:
            if tmp == S[len(S)-1].color:
                S[len(S)-1].length += 1
            else:
                S.append(Info(tmp,1))
            continue

        if tmp == S[len(S)-1].color: #最後の連と色が同じ
            S[len(S)-1].length += 1
        else: #最後の連と色が違う
            tmp_len = S[len(S)-1].length
            S.pop()
            if len(S) == 0:
                S.append(Info(tmp,tmp_len+1))
            else: #連の色は交互であるはず
                S[len(S)-1].length += tmp_len+1

    ans = 0
    for i in range(len(S)):
        if S[i].color == 0:
            ans += S[i].length

    print("%d"%(ans))

Submission status: Runtime Error

Solution code:
while (1):
    n=int(raw_input())
    if n==0:
        break
    else:
        top=-1
        fault=[0]
        S=0
        for i in range(n/2): #i=0,2,... is othello i=1,3,...is donothing
            s=int(raw_input()) #oddstone
            if top!=s: 
                fault.append(i)
                top=s
            s=int(raw_input()) #evenstone
            if top!=s:
                fault.pop()
                top=s
        i=len(fault)-1
        while i>0:
            S=S+(fault[i]-fault[i-1])*2
            i=i-2
        if not top:
            S=2*(n/2)-S
        if n%2==1:
            s=int(raw_input())
            S=S+(1-s)
        print S

Submission status: Runtime Error

Solution code:
f = open('input.txt')
fo = open('output.txt',"w")

while True:
    n = int(f.readline())
    if n == 0:
        f.close
        fo.close
        break
    arr = [0 for i in range(n)]
    for i in range(n):
        stone = int(f.readline())
        if i%2 == 0:
            arr[i] = stone
        else:
            for j in reversed(range(i)):
                if arr[j] != stone:
                    arr[j] = stone
                else:
                    break
            arr[i] = stone
    fo.write(arr.count(0))

Submission status: Runtime Error

Solution code:

stones = [0, 0]
n = input()
stones.append(input())
stones[0] = (stones[2] + 1) % 2
stones.append(1)
for i in range(n - 1):
  m = input()
  if i % 2 == 1:
    if stones[-2] == m:
      stones[-1] += 1
    else:
      stones.append(m)
      stones.append(1)
  else:
    if stones[-2] == m:
      stones[-1] += 1
    else:
      stones[-3] += stones[-1] + 1
      stones = stones[:-2]
ans = 0
for j in range(0, len(stones), 2):
  if stones[j] == 0:
    ans += stones[j + 1]
print ans

Submission status: Runtime Error

Solution code:

while True:
  stones = [0, 0]
  n = input()
  if not n:
    break
  stones.append(input())
  stones[0] = (stones[2] + 1) % 2
  stones.append(1)
  for i in range(n - 1):
    m = input()
    if i % 2 == 1:
      if stones[-2] == m:
        stones[-1] += 1
      else:
        stones.append(m)
        stones.append(1)
    else:
      if stones[-2] == m:
        stones[-1] += 1
      else:
        stones[-3] += stones[-1] + 1
        stones = stones[:-2]
  ans = 0
  for j in range(0, len(stones), 2):
    if stones[j] == 0:
      ans += stones[j + 1]
  print ans