公式

A - 商品の品質評価 / Product Quality Evaluation 解説 by admin

GPT 5.2 High

Overview

This is a problem where you calculate the risk score \(A_i \times B_i\) for each product and count the number of products for which this value is at least \(K\).

Analysis

Since we only need “the number of products satisfying the condition,” it is sufficient to independently evaluate each product and sum up the results. Specifically, for each product \(i\), we check whether \(A_i \times B_i \ge K\), and if true, increment the count by \(+1\).

A common mistake here is performing floating-point arithmetic like \(A_i/100\) just because the values involve percentages. However, since the problem explicitly defines “the risk score is \(A_i \times B_i\),” we can directly compare using the integer product (this also avoids floating-point errors).

Additionally, since \(N \le 2 \times 10^5\), an \(O(N)\) approach that examines each product once is more than sufficient. Conversely, performing sorting would result in \(O(N \log N)\), which is unnecessary for this problem (it would likely still pass, but it’s wasteful).

Concrete examples: - If \(K=1000\) and a product has \((A,B)=(10,150)\), then \(10 \times 150=1500 \ge 1000\), so we count it - If \((A,B)=(5,100)\), then \(5 \times 100=500 < 1000\), so we do not count it

Algorithm

  1. Read \(N, K\) from input
  2. Initialize an answer variable ans=0
  3. Repeat \(N\) times, reading \((A, B)\) for each product
  4. If \(A \times B \ge K\), then ans += 1
  5. Output ans at the end

Complexity

  • Time complexity: \(O(N)\) (just evaluating each product once)
  • Space complexity: \(O(1)\) (processing sequentially without storing all inputs)

Implementation Notes

  • Since \(K \le 10^{18}\), the product \(A \times B\) can also become large, but Python’s integers have arbitrary precision, so there is no worry about overflow (in other languages, it is important to use a 64-bit integer type).

  • Since the input can have up to \(2 \times 10^5\) lines, using sys.stdin.readline allows for faster reading.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    ans = 0
    for _ in range(N):
        A, B = map(int, input().split())
        if A * B >= K:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

投稿日時:
最終更新: