A - 料理コンテストと食材 / Cooking Contest and Ingredients 解説 by admin
Claude 4.5 OpusOverview
This is a problem where you select the top \(K\) chefs by score in a cooking contest and find the number of ingredients that all of them are commonly skilled at.
Analysis
Organizing the Problem
This problem can be divided into two major steps: 1. Select the top \(K\) people by score 2. Find the ingredients that all \(K\) selected people are commonly skilled at
How to Select the Top \(K\) People
We select \(K\) people in order of highest score, but in case of a tie, the chef with the smaller number ranks higher. This can be easily achieved using sorting.
For example, suppose \(K = 2\) and we have the following chefs: - Chef 1: Score 100, Skilled ingredients {1, 2, 3} - Chef 2: Score 100, Skilled ingredients {2, 3, 4} - Chef 3: Score 80, Skilled ingredients {1, 2}
In this case, the top 2 by score are Chef 1 and Chef 2 (in order of number since they are tied).
How to Find Common Ingredients
The ingredients that everyone is commonly skilled at can be found using the intersection of sets.
In the example above: - Chef 1’s skilled ingredients: {1, 2, 3} - Chef 2’s skilled ingredients: {2, 3, 4} - Common ingredients: {1, 2, 3} ∩ {2, 3, 4} = {2, 3}
Therefore, the answer is 2.
About Computational Complexity
- The maximum number of chefs is \(10^5\)
- The maximum total number of ingredients is \(2 \times 10^5\)
Sorting takes \(O(N \log N)\), and computing the intersection takes \(O(\sum C_i)\), which runs sufficiently fast within the constraints.
Algorithm
- Read input: Store each chef’s score \(V_i\) and set of skilled ingredients \(S_i\)
- Sort: Sort in descending order of score, and in ascending order of chef number for ties
- Get top \(K\) people: The first \(K\) people after sorting are the finalists
- Compute intersection: Start with the first chef’s ingredient set, and sequentially take the intersection with other chefs’ sets
- Output the answer: The size of the final intersection is the answer
common = skilled ingredients of the 1st chef
for i = 2 to K:
common = common ∩ (skilled ingredients of the i-th chef)
return |common|
Complexity
- Time complexity: \(O(N \log N + \sum_{i=1}^{N} C_i)\)
- \(O(N \log N)\) for sorting
- \(O(\sum C_i)\) for computing the intersection (each ingredient is processed at most once)
- Space complexity: \(O(N + \sum_{i=1}^{N} C_i)\)
- Space for storing chef information and ingredient sets
Implementation Tips
Sort key: In Python, using
key=lambda x: (-x[0], x[1])allows you to achieve descending order by score and ascending order by chef number simultaneously. The technique of using a negative sign for descending order is commonly used.Set intersection: In Python, you can easily compute it using the
&operator or theintersection()method.Efficient input processing: When reading large amounts of data, using
sys.stdin.read()to read all at once and then processing is faster.Caution with copying: It’s important to make a copy like
common = top_k[0][2].copy()to avoid modifying the original set (in this case,&=creates a new set so it’s not a problem, but it’s safer to do so).Source Code
import sys
from collections import defaultdict
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
chefs = []
for i in range(N):
V = int(input_data[idx]); idx += 1
C = int(input_data[idx]); idx += 1
ingredients = set()
for _ in range(C):
t = int(input_data[idx]); idx += 1
ingredients.add(t)
chefs.append((V, i, ingredients))
# Sort by score descending, then by chef number ascending
chefs.sort(key=lambda x: (-x[0], x[1]))
# Get top K chefs
top_k = chefs[:K]
# Find common ingredients among all K chefs
if K == 0:
print(0)
return
# Start with the first chef's ingredients and intersect with others
common = top_k[0][2].copy()
for i in range(1, K):
common &= top_k[i][2]
print(len(common))
if __name__ == "__main__":
main()
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: