#abc351c. C - Merge the balls
C - Merge the balls
Score: points
问题陈述
你有一段空序列和 个球。第 个球的大小 是 。
你将执行 次操作。 在第 次操作中,你将第 个球添加到序列的右端,然后重复以下步骤:
- 如果序列中有一个或更少的球,结束操作。
- 如果序列中最后一个球和倒数第二个球的大小不同,结束操作。
- 如果序列中最后一个球和倒数第二个球的大小相同,移除这两个球,并在序列的右端添加一个新的球,其大小等于两个被移除球的大小之和。然后,返回步骤 1 并重复该过程。
确定在 次操作后序列中剩余的球的数量。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You have an empty sequence and balls. The size of the -th ball is .
You will perform operations.
In the -th operation, you add the -th ball to the right end of the sequence, and repeat the following steps:
- If the sequence has one or fewer balls, end the operation.
- If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation.
- If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process.
Determine the number of balls remaining in the sequence after the operations.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the number of balls in the sequence after the operations.
Sample Input 1
7
2 1 1 3 5 3 3
Sample Output 1
3
The operations proceed as follows:
- After the first operation, the sequence has one ball, of size .
- After the second operation, the sequence has two balls, of sizes and in order.
- After the third operation, the sequence has one ball, of size . This is obtained as follows:
- When the third ball is added during the third operation, the sequence has balls of sizes in order.
- The first and second balls from the right have the same size, so these balls are removed, and a ball of size is added. Now, the sequence has balls of sizes .
- Again, the first and second balls from the right have the same size, so these balls are removed, and a ball of size is added, leaving the sequence with a ball of size .
- After the fourth operation, the sequence has one ball, of size .
- After the fifth operation, the sequence has two balls, of sizes and in order.
- After the sixth operation, the sequence has three balls, of sizes , , in order.
- After the seventh operation, the sequence has three balls, of sizes , , in order.
Therefore, you should print , the final number of balls in the sequence.
Sample Input 2
5
0 0 0 1 2
Sample Output 2
4
The operations proceed as follows:
- After the first operation, the sequence has one ball, of size .
- After the second operation, the sequence has one ball, of size .
- After the third operation, the sequence has two balls, of sizes and in order.
- After the fourth operation, the sequence has three balls, of sizes , , in order.
- After the fifth operation, the sequence has four balls, of sizes , , , in order.
Therefore, you should print , the final number of balls in the sequence.