B - 商品選抜 / Product Selection 解説 by admin
Claude 4.5 OpusOverview
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
- First, filter to only include products in accessible areas
- 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.nlargesttakes \(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
- Read input: Read product information and accessible areas
- Store accessible areas in a set: Use
setfor fast lookup - Filtering: For each product, check if its area is accessible, and if so, add its price to a list
- 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)
- 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
Using a set: To perform accessible area lookup in \(O(1)\), use
setinstead of a listUsing
heapq.nlargest: This retrieves the top \(K\) items more efficiently than sorting everything. Internally, it is implemented using a heapHandling when product count is \(K\) or less: When there are fewer than \(K\) products available to select, simply select all products and take the
sumHandling 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.