A - サイコロの転がし操作 / Dice Rolling Operations Editorial by admin Editorial for printing
GLM 5.2 (High, OpenRouter)Overview
In this problem, there are \(N\) dice, and we need to simulate \(Q\) rolling operations as instructed and output the final position and top face number of each die.
Analysis
The key point to solving this problem is that the dice do not affect each other. Although multiple dice can be placed on the same square, they are treated independently. Therefore, we can simply manage the state (position, top face, front face) of each die separately using arrays.
Each operation is performed on only one specific die, and the state changes are explicitly given by mathematical formulas. Although the constraints are relatively large with \(N+Q \leq 2\times 10^5\), if we can perform the update per operation in \(O(1)\) time, the overall process will be fast enough.
Therefore, there is no need for complex data structures or algorithms; we can solve this problem by directly implementing the update rules given in the problem statement to simulate the process.
Algorithm
- Store the initial position
pos, top facetop, and front facefrontof the \(N\) dice in arrays. - Process the \(Q\) operations sequentially. In the \(j\)-th operation, retrieve the current top face \(u\) and front face \(f\) of die \(d_j\).
- Depending on the rolling direction \(c_j\), update the state of die \(d_j\) as follows:
- For the
+direction:- Increase the position by \(1\)
- Update the top face to \(7-f\)
- Update the front face to \(u\)
- For the
-direction:- Decrease the position by \(1\)
- Update the top face to \(f\)
- Update the front face to \(7-u\)
- For the
- After all operations are completed, output the final position
pos[i]and top facetop[i]of each die in order.
Complexity
- Time Complexity: \(O(N + Q)\)
- Space Complexity: \(O(N)\)
Implementation Notes
In Python, a large I/O volume can become an execution time bottleneck, so reading all inputs at once using
sys.stdin.buffer.read().split()helps speed up execution.Since string data read with
read().split()is treated as byte strings, direction checks should be performed by comparing against byte strings, e.g.,c == b'+'.Since the dice numbers are 1-indexed, setting the array size to \(N+1\) and using 1-based indexing makes matching with the input easier.
Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
idx = 0
n = int(data[idx]); idx += 1
m = int(data[idx]); idx += 1
q = int(data[idx]); idx += 1
pos = [0] * (n + 1)
top = [0] * (n + 1)
front = [0] * (n + 1)
for i in range(1, n + 1):
pos[i] = int(data[idx]); idx += 1
top[i] = int(data[idx]); idx += 1
front[i] = int(data[idx]); idx += 1
for _ in range(q):
d = int(data[idx]); idx += 1
c = data[idx]; idx += 1
u = top[d]
f = front[d]
if c == b'+':
pos[d] += 1
top[d] = 7 - f
front[d] = u
else:
pos[d] -= 1
top[d] = f
front[d] = 7 - u
out = [None] * n
for i in range(1, n + 1):
out[i - 1] = f"{pos[i]} {top[i]}"
sys.stdout.write("\n".join(out))
main()
This editorial was generated by or-glm-5.2-high.
posted:
last update: