公式

B - 商品選抜 / Product Selection 解説 by admin 印刷ページ

Claude 4.5 Opus

Overview

This is a problem about maximizing the total by selecting at most \(K\) products with the highest selling prices from products in accessible areas.

Analysis

Key Insight

What we want to maximize in this problem is “the total selling price of selected products.” Since there is an upper limit \(K\) on the number of products that can be displayed, prioritizing products with higher prices is optimal.

Examining the Naive Approach

  1. First, filter to only include products in accessible areas
  2. Sort those products by price and select the top \(K\)

This method yields the correct answer.

Optimization Points

  • For determining accessible areas, using a set allows \(O(1)\) lookup
  • Sorting everything takes \(O(N \log N)\), but since we only need the top \(K\), using heapq.nlargest takes \(O(N \log K)\) (advantageous when \(K\) is small)

Verification with a Concrete Example

Consider the following input: - Products: (price=100, area=1), (price=200, area=2), (price=150, area=1), (price=300, area=3) - Accessible areas: {1, 3} - \(K = 2\)

Products in accessible areas: price 100 (area 1), price 150 (area 1), price 300 (area 3) Selecting the top 2: 300 + 150 = 450 is the answer.

Algorithm

  1. Read input: Read product information and accessible areas
  2. Store accessible areas in a set: Use set for fast lookup
  3. Filtering: For each product, check if its area is accessible, and if so, add its price to a list
  4. Select top \(K\) items:
    • If the number of products is \(K\) or less, select all of them
    • Otherwise, get the top \(K\) using nlargest(K, list)
  5. Output the total: Output the sum of prices of selected products

Complexity

  • Time Complexity: \(O(N + Q + N \log K)\)

    • \(O(N)\): Filtering products (set lookup is \(O(1)\) for each product)
    • \(O(Q)\): Building the set of accessible areas
    • \(O(N \log K)\): Getting top \(K\) using nlargest
  • Space Complexity: \(O(N + Q)\)

    • \(O(N)\): Product information and list of available product prices
    • \(O(Q)\): Set of accessible areas

Implementation Notes

  1. Using a set: To perform accessible area lookup in \(O(1)\), use set instead of a list

  2. Using heapq.nlargest: This retrieves the top \(K\) items more efficiently than sorting everything. Internally, it is implemented using a heap

  3. Handling when product count is \(K\) or less: When there are fewer than \(K\) products available to select, simply select all products and take the sum

  4. Handling large input: Using sys.stdin.read() to read all input at once speeds up input processing

Source Code

import sys
from heapq import nlargest

def main():
    input_data = sys.stdin.read().split()
    idx = 0
    N = int(input_data[idx]); idx += 1
    M = int(input_data[idx]); idx += 1
    K = int(input_data[idx]); idx += 1
    Q = int(input_data[idx]); idx += 1
    
    products = []
    for i in range(N):
        V = int(input_data[idx]); idx += 1
        C = int(input_data[idx]); idx += 1
        products.append((V, C))
    
    accessible = set()
    for j in range(Q):
        S = int(input_data[idx]); idx += 1
        accessible.add(S)
    
    # Collect prices of products in accessible areas
    available_values = []
    for V, C in products:
        if C in accessible:
            available_values.append(V)
    
    # Calculate the sum of top K items
    if len(available_values) <= K:
        print(sum(available_values))
    else:
        # Efficiently get top K using nlargest
        top_k = nlargest(K, available_values)
        print(sum(top_k))

if __name__ == "__main__":
    main()

This editorial was generated by claude4.5opus.

投稿日時:
最終更新: