公式

A - 中古車の選定 / Used Car Selection 解説 by admin

GPT 5.2 High

Overview

For each used car, determine whether it satisfies both “price \(L \le P_i \le R\)” and “mileage \(K_i \le M\)”, and count the number of cars that meet the conditions.

Analysis

What we need to do in this problem is extremely simple: independently check whether each car satisfies the conditions and count them.
No comparison between cars or optimization is needed, and the conditions use “AND (both must be satisfied)”.

  • For example, when \(L=200, R=300, M=50000\):
    • \((P,K)=(250, 40000)\) satisfies both conditions, so we count it
    • \((P,K)=(150, 40000)\) has a price out of range, so we don’t count it
    • \((P,K)=(250, 60000)\) has mileage exceeding the limit, so we don’t count it

Since \(N \le 10^5\), a straightforward approach (brute force) that checks each car once is more than sufficient.
Conversely, there is no need to use sorting or complex data structures here (it would only make the implementation heavier without any computational benefit).

Algorithm

  1. Read \(N, L, R, M\) from input
  2. Prepare a variable ans = 0 for the answer
  3. For each of the \(N\) cars, read \((P_i, K_i)\):
    • If \(L \le P_i \le R\) and \(K_i \le M\), increment ans by 1
  4. Output ans at the end

Complexity

  • Time complexity: \(O(N)\) (just checking each car once)
  • Space complexity: \(O(1)\) (processing input sequentially without holding any large arrays)

Implementation Tips

  • Writing the condition as L <= P <= R and K <= M in a single expression reduces the chance of mistakes (don’t forget the and).

  • Since \(N\) can be up to \(10^5\), in Python it is safer to use fast input with sys.stdin.buffer.read().split().

    Source Code

import sys

def main():
    it = iter(sys.stdin.buffer.read().split())
    N = int(next(it))
    L = int(next(it))
    R = int(next(it))
    M = int(next(it))

    ans = 0
    for _ in range(N):
        P = int(next(it))
        K = int(next(it))
        if L <= P <= R and K <= M:
            ans += 1

    sys.stdout.write(str(ans))

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.