- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 58 lines of Python from the credited upstream file abc330_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.
Use this to learn the idea, then write your own version.
12 3 4from bisect import bisect_left, bisect_right5from typing import List6 7 8def bisect_le(sorted_array: List[int], value: int):9 """Find the largest element <= x and its index, or None if it doesn't exist."""10 11 if sorted_array[0] <= value:12 index: int = bisect_right(sorted_array, value) - 113 14 return index, sorted_array[index]15 16 return None, None17 18 19def bisect_ge(sorted_array: List[int], value: int):20 """Find the smallest element >= x and its index, or None if it doesn't exist."""21 22 if sorted_array[-1] >= value:23 index: int = bisect_left(sorted_array, value)24 25 return index, sorted_array[index]26 27 return None, None28 29 30def main():31 import sys32 33 input = sys.stdin.readline34 35 d = int(input())36 upper = 15 * 10**537 ans = 10**1838 ys = [i**2 for i in range(upper + 1)]39 40 for x in range(upper + 1):41 diff = x**2 - d42 43 if diff < 0:44 diff_abs = abs(diff)45 46 _, y_min = bisect_le(ys, diff_abs)47 _, y_max = bisect_ge(ys, diff_abs)48 49 ans = min(ans, abs(diff + y_min), abs(diff + y_max))50 else:51 ans = min(ans, abs(diff))52 53 print(ans)54 55 56if __name__ == "__main__":57 main()58