102561: [AtCoder]ABC256 B - Batters
Description
Score : $200$ points
Problem Statement
Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.
There are $4$ squares called Square $0$, Square $1$, Square $2$, and Square $3$. Initially, all squares are empty.
There is also an integer $P$; initially, $P = 0$.
Given a sequence of positive integers $A = (A_1, A_2, \dots, A_N)$, perform the following operations for $i = 1, 2, \dots, N$ in this order:
- Put a piece on Square $0$.
- Advance every piece on the squares $A_i$ squares ahead. In other words, if Square $x$ has a piece, move the piece to Square $(x + A_i)$.
If, however, the destination square does not exist (i.e. $x + A_i$ is greater than or equal to $4$) for a piece, remove it. Add to $P$ the number of pieces that have been removed.
Print the value of $P$ after all the operations have been performed.
Constraints
- $1 \leq N \leq 100$
- $1 \leq A_i \leq 4$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $A_1$ $A_2$ $\dots$ $A_N$
Output
Print the value of $P$ after all the operations have been performed.
Sample Input 1
4 1 1 3 2
Sample Output 1
3
The operations are described below. After all the operations have been performed, $P$ equals $3$.
- The operations for $i=1$:
- Put a piece on Square $0$. Now, Square $0$ has a piece.
- Advance every piece on the squares $1$ square ahead. After these moves, Square $1$ has a piece.
- The operations for $i=2$:
- Put a piece on Square $0$. Now, Squares $0$ and $1$ have a piece.
- Advance every piece on the squares $1$ square ahead. After these moves, Squares $1$ and $2$ have a piece.
- The operations for $i=3$:
- Put a piece on Square $0$. Now, Squares $0$, $1$, and $2$ have a piece.
- Advance every piece on the squares $3$ squares ahead.
Here, for the pieces on Squares $1$ and $2$, the destination squares do not exist (since $1+3=4$ and $2+3=5$), so remove these pieces and add $2$ to $P$. $P$ now equals $2$. After these moves, Square $3$ has a piece.
- The operations for $i=4$:
- Put a piece on Square $0$. Now, Squares $0$ and $3$ have a piece.
- Advance every piece on the squares $2$ squares ahead.
Here, for the piece on Square $3$, the destination square does not exist (since $3+2=5$), so remove this piece and add $1$ to $P$. $P$ now equals $3$.
After these moves, Square $2$ has a piece.
Sample Input 2
3 1 1 1
Sample Output 2
0
The value of $P$ may not be updated by the operations.
Sample Input 3
10 2 2 4 1 1 1 4 2 2 1
Sample Output 3
8
Input
Output
问题描述
高桥想创造一个棒球启发的游戏,但是他很难编写代码。
为高桥写一个程序,解决以下问题。
有四个称为 Square 0,Square 1,Square 2 和 Square 3 的正方形。起初,所有的正方形都是空的。
还有一个整数 P;起初,P = 0。
给定一个正整数序列 A = (A_1, A_2, \dots, A_N),按照以下顺序执行以下操作,对于 i = 1, 2, \dots, N:
- 在 Square 0 放一个棋子。
- 将每个位于 Square $A_i$ 的棋子向前移动 $A_i$ 个正方形。也就是说,如果 Square $x$ 有一个棋子,将棋子移动到 Square $(x + A_i)$。
然而,如果一个棋子的目的地正方形不存在(即 $x + A_i$ 大于或等于 4),则移除它。将移除的棋子数量添加到 P 中。
在执行完所有操作后,打印 P 的值。
约束条件
- $1 \leq N \leq 100$
- $1 \leq A_i \leq 4$
- 输入中的所有值都是整数。
输入
输入通过标准输入以以下格式给出:
$N$ $A_1$ $A_2$ $\dots$ $A_N$
输出
在执行完所有操作后,打印 P 的值。
样例输入 1
4 1 1 3 2
样例输出 1
3
下面是操作的描述。执行完所有操作后,P 等于 3。
- 对于 i=1:
- 在 Square 0 放一个棋子。现在,Square 0 有一个棋子。
- 将每个位于 Square $1$ 的棋子向前移动 $1$ 个正方形。在这些移动后,Square $1$ 有一个棋子。
- 对于 i=2:
- 在 Square 0 放一个棋子。现在,Squares $0$ 和 $1$ 有一个棋子。
- 将每个位于 Square $1$ 的棋子向前移动 $1$ 个正方形。在这些移动后,Squares $1$ 和 $2$ 有一个棋子。
- 对于 i=3:
- 在 Square 0 放一个棋子。现在,Squares $0$, $1$ 和 $2$ 有一个棋子。
- 将每个位于 Square $3$ 的棋子向前移动 $3$ 个正方形。
对于位于 Squares $1$ 和 $2$ 的棋子,目的地正方形不存在(因为 $1+3=4$ 和 $2+3=5$),所以移除这些棋子并将 2 添加到 P。现在 P 等于 2。 在这些移动后,Square $3$ 有一个棋子。
- 对于 i=4:
- 在 Square 0 放一个棋子。现在,Squares $0$ 和 $3$ 有一个棋子。
- 将每个位于 Square $2$ 的棋子向前移动 $2$ 个正方形。
对于位于 Square $3$ 的棋子,目的地正方形不存在(因为 $3+2=5$),所以移除这个棋子并将 1 添加到 P。现在 P 等于 3。
在这些移动后,Square $2$ 有一个棋子。
样例输入 2
3 1 1 1
样例输出 2
0
P 的值可能不会因操作而更新。
样例输入 3
10 2 2 4 1 1 1 4 2 2 1
样例输出 3
8