Skip to main content

Fair Elections January Challenge 2021 Division 3 FAIRELCT

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 N packs containing A1,A2,,AN votes, and for Jack Johnson, there are M packs containing B1,B2,,BM 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 T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and M.
  • The second line contains N space-separated integers A1,A2,,AN.
  • The third line contains M space-separated integers B1,B2,,BM.

Output

For each test case, print a single line containing one integer ― the smallest number of swaps needed to make John Jackson win, or 1 if it is impossible.

Constraints

  • 1T103
  • 1N,M103
  • 1Ai106 for each valid i
  • 1Bi106 for each valid i
  • the sum of N over all test cases does not exceed 104
  • the sum of M over all test cases does not exceed 104

Subtasks

Subtask #1 (20 points):

  • A1=A2==AN
  • B1=B2==BM

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 2 votes from A and a pack of 5 votes from B. After that, John Jackson gets 5+5=10 votes and Jack Johnson gets 2+2+5=9 votes.

Example case 2: We can swap the pack of 1 vote from A and the pack of 8 votes from B. After that, John Jackson gets 8+3+2+4=17 votes and Jack Johnson gets 6+7+1=14 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)

            

   

6★vegann

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

Popular posts from this blog

Leetcode 424. Longest Repeating Character Replacement. Python (Sliding Window)

  424 .  Longest Repeating Character Replacement You are given a string  s  and an integer  k . You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most  k  times. Return  the length of the longest substring containing the same letter you can get after performing the above operations .   Example 1: Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa. Example 2: Input: s = "AABABBA", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4.   Constraints: 1 <= s.length <= 10 5 s  consists of only uppercase English letters. 0 <= k <= s.length Solution :  class Solution: def characterReplacement(self, s: str, k: int) -> int: hm = {} ans = 0

Leetcode 371. Sum of Two Integers. C++ / Java

371 .  Sum of Two Integers   Given two integers  a  and  b , return  the sum of the two integers without using the operators   +   and   - .   Example 1: Input: a = 1, b = 2 Output: 3 Example 2: Input: a = 2, b = 3 Output: 5   Constraints: -1000 <= a, b <= 1000 Solution :  C++ : class Solution { public: int getSum(int a, int b) { if (b==0) return a; int sum = a ^ b; int cr = (unsigned int) (a & b) << 1; return getSum(sum, cr); } }; Java :  class Solution { public int getSum(int a, int b) { while(b != 0){ int tmp = (a & b) << 1; a = a ^ b; b = tmp; } return a; } } Explaination :

Leetcode 242. Valid Anagram. Python (3 Ways)

  242 .  Valid Anagram Given two strings  s  and  t , return  true   if   t   is an anagram of   s , and   false   otherwise . An  Anagram  is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.   Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false   Constraints: 1 <= s.length, t.length <= 5 * 10 4 s  and  t  consist of lowercase English letters.   Follow up:  What if the inputs contain Unicode characters? How would you adapt your solution to such a case? class Solution: def isAnagram(self, s: str, t: str) -> bool: return sorted(s) == sorted(t) # return Counter(s) == Counter(t) # cs, ct = {}, {} # if len(s) != len(t): # return False # for i in range(len(s)): # cs[s[i]] = 1 + cs.get(s[i], 0) #