Official

A - 中古車の選定 / Used Car Selection Editorial by admin Editorial for printing

Qwen3-Coder-480B

Overview

Given the price and mileage of each car, the problem asks to count the number of cars that fall within specified ranges.

Analysis

In this problem, we need to determine for each car whether its “price is between \(L\) and \(R\) inclusive” and its “mileage is at most \(M\)”, then count the number of cars satisfying these conditions.

A straightforward approach is to check the conditions for every car sequentially. Since we only perform constant-time checks for each of the \(N\) cars, this is sufficiently fast. Looking at the constraints, \(N\) is at most \(10^5\), and since each car’s check takes \(O(1)\), the entire process runs in \(O(N)\). Therefore, the problem can be solved within the time limit without any special techniques.

Additionally, since each car is handled independently and no information from previous cars is needed, memory usage is minimal.

Algorithm

  1. First, read \(N\), \(L\), \(R\), \(M\) from the input.
  2. For each car, read its price \(P_i\) and mileage \(K_i\), then check the following two conditions:
    • \(L \leq P_i \leq R\)
    • \(K_i \leq M\)
  3. If a car satisfies both conditions, increment the count by 1.
  4. After checking all cars, output the count.

Complexity

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

Implementation Notes

  • The condition checks can be done with simple comparison operators, so writing them together in an if statement keeps the code concise.

  • When reading input line by line, using map(int, input().split()) allows efficient extraction of values.

  • The counter variable must be initialized outside the loop.

    Source Code

# 入力の読み込み
N, L, R, M = map(int, input().split())
count = 0

# 各車の情報を読み込んで条件をチェック
for _ in range(N):
    P, K = map(int, input().split())
    if L <= P <= R and K <= M:
        count += 1

# 候補車の台数を出力
print(count)

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

posted:
last update: