Official

A - バスの出発時刻 / Bus Departure Time Editorial by admin

Gemini 3.0 Flash

Overview

Given the times at which \(N\) students board a bus, the problem asks us to determine the time when everyone has finished boarding (= the latest arrival time) and calculate the time \(K\) minutes after that.

Analysis

The most important thing in this problem is to consider what “the time when the last student boards the bus” means.

Student \(i\) boards at time \(T_i\). The state where everyone has boarded is reached at the time corresponding to the largest value (maximum) among all \(T_1, T_2, \dots, T_N\).

For example, consider the case where the students’ arrival times are \(10, 50, 30\) and the waiting time \(K\) is \(15\). 1. At time \(10\), the 1st student boards. 2. At time \(30\), the 3rd student boards. 3. At time \(50\), the 2nd student boards, and now everyone is on board. 4. The bus departs \(15\) minutes later at time \(65\).

In this way, we can derive the answer simply by finding the maximum value from the input list of times and adding \(K\) to it.

Algorithm

  1. Read the number of students \(N\) and the waiting time \(K\) from input.
  2. Read the \(N\) times \(T_1, T_2, \dots, T_N\) as a list.
  3. Find the maximum value \(T_{max}\) in the list.
  4. Calculate and output \(T_{max} + K\).

Complexity

  • Time complexity: \(O(N)\)
    • Finding the maximum among \(N\) elements takes \(O(N)\). Since \(N \leq 10^5\), this runs sufficiently fast.
  • Space complexity: \(O(N)\)
    • If the \(N\) input times are stored in a list, \(O(N)\) memory is used.

Implementation Notes

  • Handling large input: Since \(N\) can be as large as \(10^5\), in Python it may be faster to read all input at once using sys.stdin.read().split() rather than calling input() repeatedly.

  • Finding the maximum: In Python, the built-in function max() can be used to efficiently obtain the maximum value in a list.

    Source Code

import sys

def main():
    # 全ての入力を読み込み、空白文字で分割してリストにする
    input_data = sys.stdin.read().split()
    
    if not input_data:
        return
    
    # 生徒の人数 N と、待ち時間 K を取得
    # N = int(input_data[0]) # N は直接計算には使用しない
    K = int(input_data[1])
    
    # 各生徒の乗り込み時刻 T_i は 2 番目以降の要素
    # それらの中から最大値(最後の生徒の時刻)を探す
    T_max = max(map(int, input_data[2:]))
    
    # 出発時刻は 最後の生徒の時刻 + K
    print(T_max + K)

if __name__ == '__main__':
    main()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: