Editorial for COI '08 #1 Izbori
Submitting an official solution before solving the problem yourself is a bannable offence.
The first subtask (finding the largest number of parliament seats for a party) is solved by simulation. For each party, we assume it receives all outstanding votes and then use the D'Hondt method to calculate the number of seats won. The time complexity is .
The second subtask is more difficult. The solution is based on dynamic programming and binary search.
For each party, we are asked to calculate the smallest number of seats it can win, assuming the least favourable distribution of remaining votes. Parties that initially have less than of votes can win zero parliament seats so it is not necessary to perform further calculations for them.
If the remaining votes can be distributed so that a party wins seats, then they can also be distributed so that the party gets votes (up to the maximum calculated in the first subtask). We can use binary search to find the smallest such , if we can answer the question "Is it possible to distribute the outstanding votes to other parties so that this party wins or fewer seats?"
That question can be answered by a dynamic programming algorithm. The function is the least number of votes we need to add to parties up to and including so that all of them would win seats in the parliament. In the end, all parties involved in the allocation of seats must have received at least of all votes (meaning that at most of them will be considered by the algorithm). The complexity of this DP check is about steps. There will be at most checks for at most parties, so the overall complexity is about .
Comments