A - 時刻の正規化 / Time Normalization 解説 by admin
Qwen3-Coder-480BOverview
A problem where you convert given “hour” and “minute” pairs into correct date/time format (days later, hours, minutes).
Approach
This is a fundamental problem that can be solved using unit conversion and quotient/remainder calculations.
First, since the minute value \(M\) can be input as \(60\) or greater, we need to properly convert this into “carry-over to hours.”
For example, \(80\) minutes is \(1\) hour and \(20\) minutes, so we add \(80 \div 60 = 1\) to the hours and use the remainder \(80 \bmod 60 = 20\) as the new minute value.
Next, if the updated hour value \(H'\) is \(24\) or greater, this means there is a “carry-over to days.”
For example, \(25\) hours is \(1\) day and \(1\) hour, so \(25 \div 24 = 1\) day with a remainder of \(25 \bmod 24 = 1\) hour.
In this way, by sequentially performing quotient (//) and remainder (%) calculations, we can obtain the normalized days, hours, and minutes.
Note that according to the constraints, \(H, M\) can be up to \(10^9\), so a naive simulation (e.g., incrementing by one minute in a loop) will result in TLE. A mathematical approach that computes the answer directly is required.
Algorithm
We normalize each time data using the following steps:
Carry-over from minutes
- Set the new hours \(H'\) to \(H + \left\lfloor \frac{M}{60} \right\rfloor\).
- Set the new minutes \(m\) to \(M \bmod 60\).
Carry-over from hours
- Set the number of days \(D\) to \(\left\lfloor \frac{H'}{24} \right\rfloor\).
- Set the normalized hours \(h\) to \(H' \bmod 24\).
Finally, output \(D\), \(h\), \(m\).
Concrete Example
For input 25 80:
- Carry-over from minutes: \(H' = 25 + \left\lfloor \frac{80}{60} \right\rfloor = 25 + 1 = 26\), \(m = 80 \bmod 60 = 20\)
- Carry-over from hours: \(D = \left\lfloor \frac{26}{24} \right\rfloor = 1\), \(h = 26 \bmod 24 = 2\)
Therefore the output is 1 2 20
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Since each data point can be processed in constant time, the overall solution runs in linear time.
Implementation Notes
Quotient and remainder calculations are sufficiently fast with integer arithmetic.
Since the input range is very large (up to \(10^9\)), do not simulate using loops or similar approaches.
Organizing variable names like \(H', h, m, D\) and making their meanings clear will improve code readability.
Source Code
N = int(input())
for _ in range(N):
H, M = map(int, input().split())
# 分の繰り上げ
H += M // 60
m = M % 60
# 時の繰り上げ
D = H // 24
h = H % 24
print(D, h, m)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: