Official

B - Batters Editorial by en_translator


This problem requires implementation with appropriate use of for-loops and conditional branches. You can get accepted for this problem by correctly simulating how the pieces move and how the score is updated.

There are some approaches for the simulation. One typical way is to store “which square has a piece?” as a 0/1 value (or a boolean value taking True/False) in a Python list (or a array or std::vector in C++) and update it according to \(a_i\), as in the sample code in Python below.

We will explain the analysis. It is often the case that “actually simulating result in TLE (Time Limit Exceed)” in this kind of problem, but this time, \(N\) is as small as \(N=100\), and moving pieces requires an \(\mathrm{O}(1)\) time, so the whole simulation can be completed in a total of \(\mathrm{O}(N)\) time, which is fast enough to get AC (Accepted).

  • Sample code (Python)
N = int(input())
A = list(map(int, input().split()))
P = 0
field = [0, 0, 0, 0]        # list to manage the state of squares

for x in A:
  next_field = [0, 0, 0, 0] # list to manage the next state of squares
  field[0] = 1              # Place a piece on Square 0
  for i in range(4):
    if field[i] == 1:
      if i + x >= 4:        # contional branch depending on i + x >= 4
        P += 1
      else:
        next_field[i + x] = 1
  field = next_field

print(P)

posted:
last update: