Official

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

gpt-6-astra-medium

Overview

Find the maximum number of visitors and output the first day on which this maximum value occurs. Even if there are multiple days with the maximum number of visitors, you must choose the earliest day.

Analysis

What we need to find is not the maximum number of visitors itself, but the first day on which that maximum value was recorded.

For example, if the number of visitors is \([3, 8, 5, 8, 2]\), the maximum value is \(8\). The maximum value was recorded on Day \(2\) and Day \(4\), so the answer is the earlier one, Day \(2\).

A straightforward approach of checking all days in order easily runs in time. However, if you update the answer day every time you find a value equal to the maximum, you will end up choosing the last occurrence, resulting in an incorrect answer.

In this solution, we satisfy the condition using the following Python features:

  • max(a): Finds the maximum value in list a.
  • a.index(m): Finds the first position where the value m appears.

By first finding the maximum value and then searching for the first position where that value appears, we can guarantee choosing the earliest day.

Algorithm

  1. Read the number of days \(N\) and the list a containing the number of visitors for each day.
  2. Find the maximum number of visitors m using m = max(a).
  3. Find the first position where the maximum value appears using a.index(m).
  4. Since list indices start from \(0\), add \(1\) to convert it to the day number and output it.

Since the maximum value m is guaranteed to exist in the list, and index returns its first occurrence, this method correctly finds the required day.

Complexity

  • Time Complexity: \(O(N)\)
    Both max(a) and a.index(m) examine at most \(N\) elements each. In total, it is \(O(N)\).
  • Space Complexity: \(O(N)\)
    The number of visitors for each day is stored in a list. The additional space used besides the list is \(O(1)\).

Implementation Notes

  • Pay attention to the difference between indices and day numbers. a.index(m) is \(0\)-indexed, so you must add + 1 when printing the answer.

  • Since index returns the first matching position, no special handling is required even if there are multiple occurrences of the maximum value.

  • Since \(N \geq 1\), the list is never empty, so max(a) can be used directly.

    Source Code

# 問題文から独立に実装: 最大値を求めてから、最初にその値が現れる位置を探す
n = int(input())
a = list(map(int, input().split()))
m = max(a)
print(a.index(m) + 1)

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

posted:
last update: