gRanks
Time limit5sMemory limit512 MB
Rank athletes by summing each athlete's top M weighted placement scores and ordering by total, breaking ties alphabetically.
- Level
Easy3 of 10
- Topics
- Sorting, Hash map, Implementation
- Solved
- No attempts yet
Problem
The world has many great athletes. When different athletes win different competitions, it is hard to say who is the best. Here is one way to rank them.
- Fix the number of finishing places that are worth points, and the number of points awarded for place . For example, with you could award 1000 points for first place, 500 for second place, 300 for third place, and 0 for anything below that. There are no ties within a competition.
- Competitions are not equally important, so competition gets a weight . The score an athlete gains in that competition is the points from step 1 multiplied by the weight of the competition. For example, if the Olympics has weight 5, then in the example above the winner of the Olympics gains points.
- So that an athlete does not gain simply by entering many competitions, only the highest scores an athlete earned across all competitions are added up. For example, if and an athlete earned , and points in three competitions, only 5000 and 900 are added, for a total of 5900.
You are given the points per place, the weight of each competition, and the results of the competitions. Rank every athlete who appears in the competitions.
Input
The first line contains the number of test cases . Then test cases follow. Each test case consists of the following.
- One line with the number of top places that are worth points.
- One line with the scores , starting with first place and going down.
- One line with the number of competitions.
- lines, each holding the result of one competition. A line begins with the weight of that competition and continues with the names of the athletes who finished in the top places, listed from first place downward.
- One line with the maximum number of competitions counted toward an athlete's score.
Output
For each test case, first print Case #x:, where is the test case number starting from 1. Then print every athlete who appears in the input, from the highest rank to the lowest, one per line in the format r: name, where is the rank of the athlete and name is the name.
Athletes with the same total share the same rank. The rank of an athlete is one plus the number of athletes with a strictly higher total. Athletes with the same rank are printed in alphabetical order of their names.
Constraints
- Each name uses only the uppercase letters
AthroughZand is at most 10 characters long. - No name appears twice in the result of one competition.
Hint
In the first example BOLT earned 7000 points in total from two competitions. GAY would have 8500 points if all four competitions counted, but only the top 2 count, so GAY has 6500 points and second place. PEIMENG and TIANBING both have 1500 points, so they share rank 3 and are printed in alphabetical order. LARRY has only 1000 points and comes last.