公式

A - 時刻の正規化 / Time Normalization 解説 by admin

GPT 5.2 High

Overview

Treat each reservation time \((H, M)\) as “\((60H+M)\) minutes after the reference point of 0:00”, then normalize it to \(D\) days later at \(h\) hours \(m\) minutes (\(0 \le h \le 23,\,0 \le m \le 59\)) and output the result.

Analysis

The key insight is that the carry propagation from “minutes → hours → days” can be computed in one shot using division and remainder (integer division and modulo).

  • Carry from minutes: since 1 hour equals \(60\) minutes,
    \(H' = H + \left\lfloor \dfrac{M}{60} \right\rfloor,\quad m = M \bmod 60\)
  • Carry from hours: since 1 day equals \(24\) hours,
    \(D = \left\lfloor \dfrac{H'}{24} \right\rfloor,\quad h = H' \bmod 24\)

A naive approach of “repeatedly subtract \(60\) from \(M\) and add \(1\) to \(H\) while \(M \ge 60\)” and “repeatedly subtract \(24\) from \(H\) and add \(1\) to \(D\) while \(H \ge 24\)” would result in an extremely large number of iterations since \(M, H\) can be up to \(10^9\), which would certainly TLE for \(N \le 10^5\).
Instead, we compute everything at once using // and % without any loops.

Concrete example: \((H, M) = (25, 80)\)
- \(H' = 25 + 80//60 = 25 + 1 = 26\)
- \(m = 80 \% 60 = 20\)
- \(D = 26//24 = 1\)
- \(h = 26 \% 24 = 2\)
Therefore, the result is \(1\) day later at \(2\) hours \(20\) minutes.

Algorithm

For each data point \((H, M)\), perform the following:

  1. \(H' \leftarrow H + (M // 60)\)
  2. \(m \leftarrow M \% 60\)
  3. \(D \leftarrow H' // 24\)
  4. \(h \leftarrow H' \% 24\)
  5. Output \(D, h, m\)

Repeat this independently for all \(N\) entries.

Complexity

  • Time complexity: \(O(N)\) (a constant number of operations per line)
  • Space complexity: \(O(N)\) (if storing output in an array first; \(O(1)\) is also possible with sequential output)

Implementation Notes

  • In Python, use // for integer division and % for modulo.

  • Since \(N\) can be large, reading all input at once with sys.stdin.buffer.read() and writing all output at once with "\n".join(...) is faster.

  • Computing in the order Hp -> (D, h) following the problem’s procedure reduces the chance of mistakes.

    Source Code

import sys

def main():
    it = iter(sys.stdin.buffer.read().split())
    n = int(next(it))
    out_lines = []
    for _ in range(n):
        H = int(next(it))
        M = int(next(it))
        Hp = H + M // 60
        m = M % 60
        D = Hp // 24
        h = Hp % 24
        out_lines.append(f"{D} {h} {m}")
    sys.stdout.write("\n".join(out_lines))

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

投稿日時:
最終更新: