公式

A - 山脈の最高峰 / The Highest Peak of the Mountain Range 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

Given \(N\) mountains, determine whether there exists a mountain whose elevation is strictly the maximum (strictly greater than all other mountains) — called an “independent peak.” If such a mountain exists, output its elevation; otherwise, output \(0\).

Analysis

Organizing the definition of an “independent peak,” it is a mountain that simultaneously satisfies the following two conditions:

  1. The mountain’s elevation is the maximum of all mountains
  2. There is exactly one mountain with that maximum value

For example, if the elevations are \([3, 5, 2, 5, 1]\), the maximum is \(5\), but since there are two mountains with elevation \(5\), neither is strictly greater than all other mountains (a mountain with the same elevation exists). Therefore, no independent peak exists, and the answer is \(0\).

On the other hand, for \([3, 7, 2, 5, 1]\), there is exactly one mountain with the maximum value \(7\), so mountain 2 is the independent peak, and the answer is \(7\).

When \(N = 1\), there are no other mountains to compare against, so the sole mountain is automatically an independent peak. This is naturally handled by the condition “the maximum value appears exactly once.”

In this problem, \(N\) can be as large as \(10^6\), but the only operations needed are “find the maximum” and “count occurrences of the maximum,” so scanning the array once or twice is sufficient. No special algorithm is needed, and a straightforward approach runs well within the time limit.

Algorithm

  1. Find the maximum value max_val of array \(A\)
  2. Count how many times max_val appears in the array
  3. If the count is \(1\), output max_val; otherwise, output \(0\)

Verification with concrete examples:

  • \(A = [3, 7, 2, 5, 1]\): maximum \(= 7\), count \(= 1\) → output 7
  • \(A = [3, 5, 2, 5, 1]\): maximum \(= 5\), count \(= 2\) → output 0
  • \(A = [42]\): maximum \(= 42\), count \(= 1\) → output 42

Complexity

  • Time complexity: \(O(N)\)
    • One scan of the array for max(A): \(O(N)\)
    • One scan of the array for A.count(max_val): \(O(N)\)
    • Total: \(O(N)\)
  • Space complexity: \(O(N)\) (for storing array \(A\))

Implementation Notes

  • By using Python’s built-in functions max() and list.count(), the implementation is concise and efficient. These are implemented at the C level, so they are fast enough for \(N = 10^6\).

  • By splitting the logic into two stages — first finding the maximum, then counting its occurrences — the logic stays simple. It is also possible to track both the “maximum” and “count” simultaneously in a single pass, but under this problem’s constraints, either approach works fine.

  • Pay attention to the difference between the strict inequality \(>\) (>) and the non-strict inequality \(\geq\) (>=). This problem requires strict inequality, so if the maximum value appears more than once, no independent peak exists.

    Source Code

N = int(input())
A = list(map(int, input().split()))
max_val = max(A)
if A.count(max_val) == 1:
    print(max_val)
else:
    print(0)

This editorial was generated by claude4.6opus-thinking.

投稿日時:
最終更新: