A Voting Protocol
Time limit1sMemory limit128 MB
Simulate ranked-ballot rounds where each voter backs the top unpicked candidate and the top vote getters fill k seats with alphabetical tie breaks.
- Level
Medium4 of 10
- Topics
- Simulation, Sorting
- Solved
- No attempts yet
Problem
An election fills several positions at once. Suppose there are positions to fill. Each voter hands in a ranked list of the candidates they prefer most. A candidate is identified by a single lowercase letter, the first choice sits at position 1, the second choice at position 2, and so on.
The winners are picked over a series of rounds. In each round every voter votes for the first candidate on their list who has not been selected yet. The candidate with the most votes joins the selected list. When several candidates tie for the most votes, all of them are selected, unless that would push the total past . In that case the tie is broken by alphabetical order: if two more candidates are needed and 'a', 'x' and 'z' all have equally many votes, then 'a' and 'x' are selected. The election ends once candidates have been selected.
Input
The input holds several elections. The first line of an election contains two integers: the number of positions to fill, (), and the number of voters, (). Then come lines, each a sequence of lowercase letters with no repetition, since nobody can vote for the same person twice. The input ends with a line containing two zeros.
Output
Print one line per election. Each line contains the word Election, a space, the number of the election (a running count starting at 1), a colon and a space, then the selected candidates written in the order they were selected. Candidates selected in the same round are listed alphabetically.