Official

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

gpt-6-astra-medium

Overview

Find the earliest day with the maximum number of visitors. For each day, compare its number of visitors with the other days to check whether it satisfies the conditions for the answer.

Analysis

For a day to be the answer, it must satisfy both of the following conditions:

  • There is no day before it with a number of visitors greater than or equal to that day.
  • There is no day after it with a number of visitors strictly greater than that day.

If there is an earlier day with the same number of visitors, that earlier day takes precedence, so the current day cannot be the answer. On the other hand, even if there is a later day with the same number of visitors, the current day can still be the answer.

For example, consider the case where the number of visitors is \([3, 7, 7, 2]\).

  • There is no day before Day \(2\) with \(7\) or more visitors, and there is no day after Day \(2\) with more than \(7\) visitors, so Day \(2\) is the answer.
  • Day \(3\) is not the answer because there is an earlier day (Day \(2\)) with the same number of visitors (\(7\)).

If we simply check whether “there is no day with more visitors than this day,” multiple days achieving the maximum value would satisfy the condition. We need a way to select the earliest day. In the presented approach, this condition is handled by using “greater than or equal to” when comparing with earlier days.

Comparing each day with all other days takes \(O(N^2)\) time, but since \(N \leq 100\), there are at most \(10000\) pairs of days to compare. This naive method easily runs within the time limit.

Algorithm

Assume 0-based indexing for the array.

  1. Read the number of visitors into an array a.
  2. For each index \(i = 0, 1, \ldots, N-1\), perform the following:
    1. Initialize a variable ok to true, representing whether \(i\) satisfies the conditions for the answer.
    2. Iterate through all indices \(j\):
      • If \(j < i\) and \(a_j \geq a_i\), set ok to false.
      • If \(j > i\) and \(a_j > a_i\), set ok to false.
    3. If ok is still true, output \(i+1\) and terminate.

If ok is true, there is no day with strictly more visitors, nor is there any earlier day with the same number of visitors. Therefore, this day is the “earliest day among those with the maximum number of visitors,” which is the correct answer.

Complexity

  • Time complexity: \(O(N^2)\)
    For each day, we compare it with up to \(N\) days.
  • Space complexity: \(O(N)\)
    We store the number of visitors for each day in an array.

Implementation Notes

  • Use >= for earlier days and > for later days. Due to this difference, even if there are multiple days with the maximum number of visitors, only the earliest day will satisfy the condition.

  • The array is 0-indexed, but the required output is 1-indexed, so we output i + 1.

  • If a condition is violated even once, that candidate is not the answer. Make sure not to reset ok back to true within the inner loop.

    Source Code

// 問題文から独立に実装: 各日 i について「自分より前に自分以上の日がなく、後ろに自分より大きい日がない」かを調べる
#include <iostream>
#include <vector>
int main() {
    int n; std::cin >> n;
    std::vector<int> a(n);
    for (int& x : a) std::cin >> x;
    for (int i = 0; i < n; ++i) {
        bool ok = true;
        for (int j = 0; j < n; ++j) {
            if (j < i && a[j] >= a[i]) ok = false;
            if (j > i && a[j] > a[i]) ok = false;
        }
        if (ok) { std::cout << i + 1 << std::endl; return 0; }
    }
}

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

posted:
last update: