Problem solution · Python

ABC095 D — Static Sushi

ABC095 D — Static Sushi: 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
74 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 ABC095 D — Static Sushi, 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

  • 74 lines of Python from the credited upstream file arc096_b.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 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 codeABC095 D — Static Sushi · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     input = sys.stdin.readline     n, c = map(int, input().split())    x = [0 for _ in range(n)]    v = [0 for _ in range(n)]     for i in range(n):        xi, vi = map(int, input().split())        x[i] = xi        v[i] = vi     def calc():        from itertools import accumulate         # 始めにいた位置→時計回りにaiまで移動したときの摂取カロリー        a = list(accumulate(v))        # 始めにいた位置→反時計回りにbiまで移動したときの摂取カロリー        b = list(accumulate(v[::-1]))[::-1]         # 反時計回りに移動したときの距離        y = [c - xi for xi in x]         # 反時計回りに移動したときの摂取カロリーの最大値        d = [0 for _ in range(n)]        d[n - 1] = b[n - 1] - y[n - 1]         for i in range(n - 2, -1, -1):            d[i] = b[i] - y[i]            d[i] = max(d[i], d[i + 1])         candidate = 0         # aiの位置を全探索        for i, (ai, xi) in enumerate(zip(a, x)):            sum_cal = 0            sum_cal -= xi  # walk            sum_cal += ai  # eat             candidate = max(candidate, sum_cal)             # 最初の位置まで戻る            sum_cal -= xi  # walk             if i < n - 1:                sum_cal += d[i + 1]             candidate = max(candidate, sum_cal)         return candidate     ans = 0     # O→A→O→B    result = calc()    ans = max(ans, result)     # O→B→O→A    v = v[::-1]    x = [c - xi for xi in x][::-1]    result = calc()    ans = max(ans, result)     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 ↗