B - 最初のアラート / First Alert Editorial by admin
Gemini 3.0 FlashOverview
This problem asks you to check \(N\) log entries in order and find the log number (i.e., which position) at which the severity first reaches or exceeds the threshold \(K\). Since an alert is only triggered once and subsequent entries are ignored, you need to find the smallest index that satisfies the condition.
Analysis
The key point of this problem is the rule: “stop as soon as the condition is met for the first time.”
Logs arrive in order as \(P_1, P_2, \dots, P_N\). Takahashi checks them from the beginning, and starts responding the moment the severity reaches \(K\) or higher. Once he begins responding, no further alerts are issued regardless of how important subsequent logs may be.
Therefore, the problem can be solved with the following steps: 1. Examine the logs in order from \(i = 1\) to \(N\). 2. If \(P_i \geq K\), output the number \(i\) and terminate the program. 3. If no log satisfies the condition after checking all of them, output \(-1\).
Checking the constraints, \(N \leq 2 \times 10^5\), so a “linear search” that checks each log one by one is more than fast enough.
Algorithm
- Read \(N, K\) and the array \(P\) of logs from input.
- Iterate through the elements of array \(P\) from the beginning.
- For the current element \(P_i\) in the loop, perform the conditional check:
if P_i >= K:. - If the condition is satisfied, output the current number and terminate processing with
exit()orreturn.- Note that arrays in most programming languages are 0-indexed, but the problem counts from 1 (1-indexed), so you need to add \(+1\) to the index when outputting.
- If the loop completes without the condition ever being satisfied, output \(-1\).
Complexity
- Time complexity: \(O(N)\)
- Each of the \(N\) log entries is checked at most once, so the computation finishes in time proportional to the number of logs.
- Space complexity: \(O(N)\)
- If all log severities are stored in an array, the memory usage is proportional to the input size.
Implementation Notes
Handling 1-indexed output: Be careful that array indices (0, 1, 2, …) and log numbers (1, 2, 3, …) are off by 1.
Early return: By terminating processing the moment a satisfying entry is found, you avoid unnecessary computation and accurately capture only the “first alert.”
Fast I/O: In Python, when the number of inputs \(N\) is around \(2 \times 10^5\), reading all input at once using
sys.stdin.read().split()can be faster than callinginput()repeatedly.Source Code
import sys
def main():
# 標準入力からすべてのデータを読み込み、空白(スペース、改行など)で分割します
input_data = sys.stdin.read().split()
# 入力が空の場合は終了します
if not input_data:
return
# 1つ目の要素がN(ログの件数)、2つ目の要素がK(閾値)です
N = int(input_data[0])
K = int(input_data[1])
# 3つ目以降の要素が各ログの重要度 P_1, P_2, ..., P_N です
# 順番にチェックし、最初に閾値 K 以上になったログの番号を出力して終了します
for i in range(N):
# 重要度 P_i は input_data[i + 2] に格納されています
if int(input_data[i + 2]) >= K:
# ログの番号は 1-indexed なので i + 1 を出力します
print(i + 1)
return
# すべてのログを確認しても閾値以上の重要度が見つからなかった場合、-1 を出力します
print(-1)
if __name__ == "__main__":
main()
This editorial was generated by gemini-3-flash-preview.
posted:
last update: