Election
SRM 361 · 2007-08-01 · by slex
Problem Statement
N candidates, indexed from 0 to N-1, participate in an election. After the voting is over, candidates are sorted by decreasing number of votes, and by increasing index in case of ties.
With the voting underway you have your favorites for different positions. You wonder how many additional voters would have to come to make your selection possible.
The
Constraints
- votes will contain between 1 and 50 elements, inclusive.
- wishList will contain the same number of elements as votes.
- Each element of votes will be between 0 and 10,000,000, inclusive.
- Each element of wishList will be between -1 and N - 1, inclusive, where N is the number of elements in votes.
- Elements of wishList, other than -1, will be distinct.
{10,8,6,4}
{-1,0,-1,2}
Returns: 6
If 3 more people vote for candidate 1, and 3 more people vote for candidate 3, the vote distribution would be {10,11,6,7}, and the final candidate ranking would be {1,0,3,2}, which matches your wishes.
{10,20,30,0}
{-1,-1,-1,0}
Returns: 31
Candidate 3 will need 31 votes to take the lead.
{10, 100, 40, 15, 15}
{4, 0, 1, 3, 2}
Returns: 1
It's almost as wished, but the tie breaker rule would make candidate 3 come before candadiate 4. One more vote is enough to fix that.
{5052863,4419975,3556783,9490441,6166389,836297,
4623556,4905465,117307,8071245,932463,9194925}
{-1,-1,-1,-1,2,0,-1,-1,8,9,4,11}
Returns: 53562445
{0}
{0}
Returns: 0
Submissions are judged against all 70 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Election with a public method int votesNeeded(vector<int> votes, vector<int> wishList) · 70 test cases · 2 s / 256 MB per case