Official

B - 祭日の来客 / Festival Visitors: The Busiest Day Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

We check the number of visitors in order starting from the first day, keeping track of the maximum number of visitors and the day it occurred. By not updating our record when an equal maximum value appears, we can find the earliest day.

Analysis

A naive approach that inspects the number of visitors on each day once is well within the time limit for this problem.

While iterating, we maintain the following information:

  • best: the maximum number of visitors among the days examined so far
  • best_day: the earliest day that recorded this maximum value

The key point is to update only when the current number of visitors is strictly greater than best.

For example, consider the case where the numbers of visitors are \([3, 7, 7, 2]\). On day \(2\), we record the maximum value as \(7\) and the day as \(2\). On day \(3\), the number of visitors is also \(7\), but by not updating the record, we can correctly answer day \(2\).

If we were to update whenever “the current number of visitors is greater than or equal to best,” the day would be overwritten every time the same maximum value appears, resulting in the latest day being answered. This would lead to a wrong answer.

Algorithm

  1. Initialize best = -1 and best_day = 1.
  2. For each \(i = 1, 2, \ldots, N\) in order, inspect the number of visitors \(A_i\):
    • If \(A_i > \text{best}\), update best to \(A_i\) and best_day to \(i\).
    • Otherwise, do nothing.
  3. Finally, print best_day.

After checking each day, best holds the maximum number of visitors up to that day. Furthermore, since we iterate in order from the first day and do not update on ties, best_day is the earliest day that recorded this maximum. Therefore, we obtain the correct answer once all days have been checked.

Complexity

  • Time Complexity: \(O(N)\)
    We inspect the number of visitors for each day once.
  • Space Complexity: \(O(N)\)
    In the provided code, the entire input is stored in data. If only counting the additional space to track the maximum value and its day, it is \(O(1)\).

Implementation Notes

  • Since the number of visitors is at least \(0\), initializing best to \(-1\) ensures that the first day’s information is always recorded.

  • Use > instead of >= for the comparison so that the earliest day is retained even if the same maximum value appears again.

  • data[0] is the number of days \(N\), and data[i] is the number of visitors on day \(i\). By using range(1, n + 1), the loop index i can be used directly as the day to output.

    Source Code

import sys

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    best_day = 1
    best = -1
    for i in range(1, n + 1):
        a = int(data[i])
        if a > best:
            best = a
            best_day = i
    print(best_day)

main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: