Official

B - 最初のアラート / First Alert Editorial by admin

Qwen3-Coder-480B

Overview

Find the number of the first log whose importance reaches the threshold \(K\) or above. If none exists, output \(-1\).

Discussion

In this problem, \(N\) logs are given in order, each with an assigned importance \(P_i\). Takahashi issues an alert as soon as he finds a log with importance \(K\) or above, and completely ignores all subsequent logs after that.

Therefore, we simply need to scan from the beginning and find the first \(i\) such that \(P_i \geq K\). Once found, output that index (1-indexed) and terminate the program. If no such log is found after checking all of them, output \(-1\).

A straightforward approach is sufficiently fast, since at most \(N\) checks are needed (\(N \leq 2 \times 10^5\)). No pruning or complex data structures are required.

Algorithm

  1. Read \(N\), \(K\), and the importance values \(P_1, P_2, ..., P_N\) from input.
  2. Scan the list \(P\) from the beginning and find the first index \(i\) where \(P_i \geq K\).
  3. If found, output that index (1-indexed) and terminate the program.
  4. If no such log is found after checking all logs, output \(-1\).

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Implementation Notes

  • Since the output must be 1-indexed, don’t forget to add \(+1\) to the index.

  • By using exit(), you can immediately terminate the program as soon as a log satisfying the condition is found.

    Source Code

N, K = map(int, input().split())
P = list(map(int, input().split()))

for i in range(N):
    if P[i] >= K:
        print(i + 1)
        exit()

print(-1)

This editorial was generated by qwen3-coder-480b.

posted:
last update: