Who Brings the Cookies?
Time limit1sMemory limit128 MB
Assign one cookie-bringing cow per study group so each cow's meeting count stays within a ceiling-of-reciprocals limit, choosing the lexicographically smallest assignment.
- Level
Hard8 of 10
- Topics
- Math, Greedy, Brute force, Implementation
- Solved
- No attempts yet
Problem
Farmer John's cows, numbered through , form study groups. Study group has member cows (a single cow may belong to several study groups).
For every study group, exactly one of its member cows must be chosen to bring cookies to that group's meeting. Cookies are expensive and take effort to prepare, so the cows want to share the burden as evenly as possible.
Suppose a cow belongs to study groups whose sizes are (that is, she is a member of groups, and the -th of them has members in total). Then she is willing to bring cookies to at most
meetings.
Assign to each study group the one cow that will bring its cookies, so that no cow is assigned more meetings than she is willing to bring.
Constraints: , , and .
Input
- Line 1: two space-separated integers and .
- Lines 2 to : line describes study group as followed by the member cow numbers , all space-separated.
Output
Print lines. Line contains , the number of the cow who brings cookies to study group .
Several valid assignments may exist, so output the lexicographically smallest one: make as small as possible; among all assignments with that , make as small as possible; and so on.
If no valid assignment exists, print a single line containing instead.
Hint
The limit counts how expensive a cow's meetings are: a member of a large group contributes little (its cost is small), so she can afford more meetings.
For instance, take a cow that belongs to groups of sizes , , and . Her limit is , so she is willing to bring cookies to at most meetings. A cow in two groups of size has limit .