题解:AtCoder AT_awc0088_a Bus Departure Time

【题目来源】

AtCoder:A - Bus Departure Time

【题目描述】

Takahashi is a chaperone teacher on a field trip, and his role is to give the departure signal after all students have boarded the bus.

There are \(N\) students participating in this field trip, and each student \(i\) \((1 \leq i \leq N)\) finishes boarding the bus at time \(T_i\). Here, time is represented as a non-negative integer in minutes, measuring the elapsed time from a certain reference point.

All students finish boarding the bus at the time equal to the maximum value among \(T_1, T_2, \ldots, T_N\). Takahashi is supposed to give the departure signal exactly \(K\) minutes after that time.

Find the time at which Takahashi gives the departure signal.

高桥是修学旅行的带队老师,他的职责是在所有学生上车后发出出发信号。

参加这次修学旅行的有 \(N\) 名学生,学生 \(i\)\(1 \leq i \leq N\))在时间 \(T_i\) 完成上车。这里,时间以分钟为单位表示为非负整数,是从某个参考点开始经过的时间。

所有学生在时间等于 \(T_1, T_2, \ldots, T_N\) 中的最大值时完成上车。高桥应该在那之后正好 \(K\) 分钟发出出发信号。

求高桥发出出发信号的时间。

【输入】

\(N\) \(K\)
\(T_1\) \(T_2\) \(\ldots\) \(T_N\)

  • The first line contains an integer \(N\) representing the number of students and an integer \(K\) representing the time in minutes from when all students finish boarding until the departure signal is given, separated by a space.
  • The second line contains integers \(T_1, T_2, \ldots, T_N\) representing the time at which each student \(i\) finishes boarding the bus, separated by spaces.

【输出】

Print the time at which Takahashi gives the departure signal, as an integer representing the elapsed time in minutes from the reference point, on a single line.

【输入样例】

3 5
10 25 15

【输出样例】

30

【核心思想】

  1. 问题分析:给定 \(N\) 个学生的上车时间 \(T_1, T_2, \ldots, T_N\),所有学生上车完毕的时间为 \(\max(T_1, T_2, \ldots, T_N)\),出发信号在该时间之后 \(K\) 分钟发出。求出发信号时间。这是一个直接模拟问题,关键在于识别"所有学生上车完毕"等价于取最大值,然后做简单加法。

  2. 算法选择

    • 线性扫描求最大值:遍历 \(N\) 个时间,维护当前最大值
    • 直接计算:结果 = 最大值 + \(K\)
  3. 关键步骤

    • 初始化:读取 \(N\)(学生数量)和 \(K\)(延迟分钟数)
    • 维护最大值
      • 初始化 maxn = -\infty(或 \(-1\),因时间非负)
      • 遍历 \(i\)\(1\)\(N\)
        • 读取 \(T_i\)
        • maxn = \max(maxn, T_i):更新最大上车时间
    • 计算并输出ans = maxn + K,输出 ans
  4. 时间/空间复杂度

    • 时间复杂度:\(O(N)\),线性扫描 \(N\) 个时间求最大值
    • 空间复杂度:\(O(1)\),仅需常数额外空间存储最大值和结果
  5. 直接模拟的核心思想

    • 问题转化:将"所有学生上车完毕"这一条件精确转化为数学上的最大值操作,避免复杂建模
    • 单次遍历:无需排序或复杂数据结构,线性扫描即可得到全局最大值
    • 无状态依赖:每个 \(T_i\) 的处理相互独立,只与当前最大值比较,适合流式处理
    • 适用场景:条件可直接映射为简单统计量(最大、最小、求和等)的模拟问题,数据规模允许线性处理

【算法标签】

模拟

【代码详解】

#include <bits/stdc++.h>
using namespace std;

// 定义长整型别名,方便处理大数据
#define int long long

// 全局变量声明
int n, k;       // n: 数字个数, k: 要加上的值
int maxn = -1;  // 用于记录最大值,初始化为最小可能值

// 主函数入口
signed main()
{
    // 读取输入的数字个数n和加数k
    cin >> n >> k;

    // 循环读取n个整数并找出最大值
    for (int i = 1; i <= n; i++)
    {
        int x;          // 临时存储当前输入的数字
        cin >> x;       // 读取一个数字
        maxn = max(maxn, x);   // 更新最大值
    }

    // 输出最大值加上k的结果
    cout << maxn + k << endl;

    return 0;
}

【运行结果】

3 5
10 25 15
30
posted @ 2026-07-04 09:37  团爸讲算法  阅读(4)  评论(0)    收藏  举报