Problem solution · Python

ABC320 C — Slot Strategy 2 (Easy)

ABC320 C — Slot Strategy 2 (Easy): a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
46 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC320 C — Slot Strategy 2 (Easy), the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 46 lines of Python from the credited upstream file abc320_c.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC320 C — Slot Strategy 2 (Easy) · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     input = sys.stdin.readline     m = int(input())    s = [list(input().rstrip()) * 10 for _ in range(3)]    inf = 10**18    ans = inf    n = 3 * m  # 所要時間の上限 = 1周で1つのリールを止める     # 全てのリールを止めるまでの時間を全探索    # 条件: 表示されている文字が全て同じ、かつ、各リールを止める時間が異なる    # 計算量: O(m ** 3)    for i in range(n):        for j in range(n):            for k in range(n):                if i == j:                    continue                if j == k:                    continue                if k == i:                    continue                 if s[0][i % m] != s[1][j % m]:                    continue                if s[1][j % m] != s[2][k % m]:                    continue                if s[2][k % m] != s[0][i % m]:                    continue                 ans = min(ans, max(i, j, k))     # 例外処理    if ans == inf:        ans = -1     print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗