Official

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

swe-2-high

Explanation

The answer is the earliest day on which the visitor count reaches its maximum. Read the values one by one while keeping track of the best value seen so far, \(\\mathrm{best}\), and the day it was achieved, \(\\mathrm{idx}\).

When reading \(A_i\) on day \(i\), if \(A_i > \\mathrm{best}\), update \(\\mathrm{best} \\leftarrow A_i\) and \(\\mathrm{idx} \\leftarrow i\). Using the strict inequality \(>\) is the key: a later day with the same count never overwrites \(\\mathrm{idx}\), so \(\\mathrm{idx}\) always holds the earliest day attaining the maximum.

Why it works

After processing day \(i\), the invariant holds that \(\\mathrm{best}\) equals \(\\max(A_1, \\ldots, A_i)\) and \(\\mathrm{idx}\) is the first index where that maximum occurs. At \(i = N\) this is exactly the required answer.

Complexity

Each day is processed once, so the running time is \(O(N)\). Only a constant number of variables is stored — the input is never kept in an array — so the space usage is \(O(1)\).