Fair Elections Problem Code: FAIRELCT
Elections are coming soon. This year, two candidates passed to the final stage. One candidate is John Jackson and his opponent is Jack Johnson.
During the elections, everyone can vote for their favourite candidate, but no one can vote for both candidates. Then, packs of votes which went to the same candidate are formed. You know that for John Jackson, there are packs containing votes, and for Jack Johnson, there are packs containing votes.
The winner is the candidate that has strictly more votes than the other candidate; if both have the same number of votes, there is no winner. You are a friend of John Jackson and you want to help him win. To do that, you may perform the following operation any number of times (including zero): choose two packs of votes that currently belong to different candidates and swap them, i.e. change the votes in each of these packs so that they would go to the other candidate.
You are very careful, so you want to perform as few swaps as possible. Find the smallest number of operations you need to perform or determine that it is impossible to make John Jackson win.
Input
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers and .
- The second line contains space-separated integers .
- The third line contains space-separated integers .
Output
For each test case, print a single line containing one integer ― the smallest number of swaps needed to make John Jackson win, or if it is impossible.
Constraints
- for each valid
- for each valid
- the sum of over all test cases does not exceed
- the sum of over all test cases does not exceed
Subtasks
Subtask #1 (20 points):
Subtask #2 (80 points): original constraints
Example Input
2
2 3
2 2
5 5 5
4 3
1 3 2 4
6 7 8
Example Output
2
1
Explanation
Example case 1: We can perform two swaps ― each time, we swap a pack of votes from and a pack of votes from . After that, John Jackson gets votes and Jack Johnson gets votes.
Example case 2: We can swap the pack of vote from and the pack of votes from . After that, John Jackson gets votes and Jack Johnson gets votes
Fair Election Codechef Solution in Python:
for tc in range(int(input())):
n,m=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
possibility=True
swap_count=0
while sum(a)<=sum(b):
a.sort()
b.sort()
if a[0]<b[-1]:
a[0],b[-1]=b[-1],a[0]
swap_count=swap_count+1
else:
print(-1)
possibility=False
break
if possibility==True:
print(swap_count)
21-12-2020
1 secs
50000 Bytes
CPP14, C, JAVA, PYTH 3.6, PYTH, CS2, ADA, PYPY, PYP3, TEXT, CPP17, PAS fpc, RUBY, PHP, NODEJS, GO, TCL, HASK, PERL, SCALA, kotlin, BASH, JS, PAS gpc, BF, LISP sbcl, CLOJ, LUA, D, R, CAML, rust, ASM, FORT, FS, LISP clisp, SQL, swift, SCM guile, PERL6, CLPS, WSPC, ERL, ICK, NICE, PRLG, ICON, PIKE, COB, SCM chicken, SCM qobi, ST, NEM, SQLQ
Comments
Post a Comment