Distance-Weighted Voting
Marlow Utilities found that plain head-counting kept booking the wrong category. A shortlisted past fault that barely resembled the new one was outvoting one that matched almost perfectly, purely because the poor matches were more numerous. So the vote is being weighted. A shortlisted fault's say is inversely proportional to how far it sits from the new fault: one twice as far away has exactly half as much say, one three times as far a third. Nobody on the shortlist is dropped, and everything said on behalf of a category is pooled together.
Task: Print the repair category with the most say behind it.
Input
The first line holds n, the number of shortlisted faults. Each of the next n lines holds a category name followed by that fault's distance from the new one, separated by a space. A name contains no spaces and may appear on several lines. Every distance is greater than zero.
Output
One line holding the winning category. A category named on several lines pools the say from all of them — every entry counts, not merely its closest one. If two categories finish with exactly equal say, print whichever comes first alphabetically.
Example:
Input:
3
A 2
B 1
A 4
Output:
B
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in