J: Draft Time
https://open.kattis.com/problems/drafttime
Last updated
https://open.kattis.com/problems/drafttime
Last updated
The algorithm used is a variant of the for stable matching:
While there exists an unmatched player who has not proposed to all teams:
Let be the most preferred team that has not proposed to
Assign to team
If was already full (i.e. already had players assigned), unassign the player that least prefers
Why does this algorithm work?
This algorithm always gives players to each team. This is because players can't be unassigned from a team until the team has other players. So, a player will only be unassigned at the end if all teams are full, and since we have a surplus of players (i.e. ), all teams will be full.
There are no instabilities after running the algorithm. Suppose toward contradiction that there were, then there exists who is undrafted or would prefer to go to team over the team he got drafted by, and can draft more players or prefers to an already drafted player. However, must have proposed to (since he is either undrafted or prefers to current choice), and must have unassigned because found more-preferred players. So, must not prefer to its already drafted players.
Note that this algorithm always completes, so there's no case where you should print Hello darkness my old friend!
For implementation, you can use priority queues per team (sorted by the team's player preferences) to store matches, a stack/queue to store unmatched players, a hash map to keep track of the number of teams proposed to by each player, and hash maps to keep track of team/player preferences.
While the stack/queue is not empty, remove a player and match them to their next team (if they haven't proposed to all teams) by inserting in the team's priority queue. Then, if the priority queue has elements, pop the top (least preferred) player and add them back to the stack/queue.