Fair Elections FAIRELCT SOLUTION Code Chef

Fair Elections FAIRELCT SOLUTION Code Chef

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 NN packs containing A1,A2,…,ANA1,A2,…,AN votes, and for Jack Johnson, there are MM packs containing B1,B2,…,BMB1,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 TT denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains two space-separated integers NN and MM.
  • The second line contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN.
  • The third line contains MM space-separated integers B1,B2,…,BMB1,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−1 if it is impossible.

Constraints

  • 1≤T≤1031≤T≤103
  • 1≤N,M≤1031≤N,M≤103
  • 1≤Ai≤1061≤Ai≤106 for each valid ii
  • 1≤Bi≤1061≤Bi≤106 for each valid ii
  • the sum of NN over all test cases does not exceed 104104
  • the sum of MM over all test cases does not exceed 104104

Subtasks

Subtask #1 (20 points):

  • A1=A2=…=ANA1=A2=…=AN
  • B1=B2=…=BMB1=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 22 votes from AA and a pack of 55 votes from BB. After that, John Jackson gets 5+5=105+5=10 votes and Jack Johnson gets 2+2+5=92+2+5=9 votes.

Example case 2: We can swap the pack of 11 vote from AA and the pack of 88 votes from BB. After that, John Jackson gets 8+3+2+4=178+3+2+4=17 votes and Jack Johnson gets 6+7+1=146+7+1=14 votes.

Solution

Program Python:

for _ in range(int(input())):
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    i=0
    x=True
    while sum(a)<=sum(b):
        a.sort()
        b.sort()
        if a[0]<b[-1]:
            temp=b[-1]
            b[-1]=a[0]
            a[0]=temp
            i+=1 
        else:
            x=False
            print(-1)
            break
    if x==True:
        print(i)

Program Java:

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
 public static void main (String[] args) throws java.lang.Exception
 {
  int t,n1,n2,k;
  Scanner sc=new Scanner(System.in);
  t=sc.nextInt();
  
 
  while(t-->0)
  {
      n1=sc.nextInt();
      n2=sc.nextInt();
      int maxmm,minmm;
     int a[]=new int[n1];
     int b[]=new int[n2];
     int sma=0,smb=0;
     for(int i=0;i<n1;i++)
     {a[i]=sc.nextInt(); sma+=a[i];}
     for(int j=0;j<n2;j++)
     {b[j]=sc.nextInt();smb+=b[j];}
     int cnt=0;
     //System.out.println(sma+"hii\n"+smb+"\n");
     Arrays.sort(a);
     Arrays.sort(b);
     int i=0,j=n2-1;
     while(i<n1 && j>=0)
     {
         if(sma>smb)
         break;
         else
         {
             minmm=a[i];
             maxmm=b[j];
             sma=sma+maxmm-minmm;
             smb=smb+minmm-maxmm;
             cnt++;
             i++;
             j--;
         }
     }
     if(sma<=smb)
     System.out.println("-1");
     else
     System.out.println(cnt);
     
      
  }
 }
}

Program C++:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;

        int johnny =0;
        int jacky = 0;
        int swap = 0;
        int arr1[n],arr2[m];
        for(int i=0;i<n;i++){
            cin>>arr1[i];
            johnny += arr1[i];
        }
        for(int i=0;i<m;i++){
            cin>>arr2[i];
            jacky += arr2[i];
        }
        if(johnny > jacky){
            cout<<"0\n";
            continue;
        }
        sort(arr1, arr1+n);
        sort(arr2, arr2+m, greater<int>());

        int i = 0;
        while(johnny <= jacky && i<n && i<m)
        {
            johnny += arr2[i] - arr1[i];
            jacky += arr1[i] - arr2[i];
            swap++;
            i++;
        }
        if(i == n && johnny <= jacky ){
            cout<<"-1\n";
        }
        else
        {
            cout<<swap<<"\n";
        }
        
    }
    return 0;
}

January Long Challenge 2021

Leave a Comment