Jollybeoj: 18-Duel Maut

by Abdillah Muhamad — on

cover-image

Problem :

A small island in Indonesia recently discovered by to groups of pirates. It turns out this island was once the secret place where pandas keep their treasure. This treasure, if converted into Rupiahs (Indonesian currency), could be up to Rp.1.000.000.000.000,- (approximately USD 100 million). These pirate groups immediately declare a war to each other after they saw the treasure. They are greedy pirates; They don’t want to share this treasure with other group. Both captain from each group agreed to resolve this dispute through a death match, an ancient tradition of the pirates.

Death match is a non-weapon duel (bare hand) participated by 2 fighters from 2 different group. Each group will send one fighter to participate in this death match. The winner of this death match has the power to settle the final decision of any problem they are fighting for. The final decision must be obeyed, otherwise they will face the King of Pirates, which will be much more scary than facing any other pirates.

Each pirate’s power can be represented by an integer number, higher number means stronger fighter. This death match will be much more entertaining if 2 participating pirates have similar power. Given data of each pirate’s power from both groups, determine the smallest difference of powers between 2 groups (1 from each group) that can be found.

Example. Pirate group A have 5 fighters: 10, 9, 41, 17, 24. Pirate group B have 4 fighters: 50, 19, 29, 51.

This table show us the difference of each pair of fighter from group A and group B.

18 Duel Maut

From this table, we can see that the smallest difference is 2, if fighter with power 17 from A fights with fighter with power 19 from B.

Input

First row consists of a number T (T ≤ 100), the number of test cases. Each case starts with 2 integers N dan M (1 ≤ N, M ≤ 100), the number of fighters in group A, and the number of fighters in group B. Next row consists of N integers Ai (1 ≤ Ai ≤ 1.000), the power of fighters from group A. Next row consists of M integers Bi (1 ≤ Bi ≤ 1.000), the power of fighters from group B.

Output

For each case, output in a line “Kasus #X: Y” (without quotes), with X represents a case number starts from 1, and Y represents the smallest difference between fighter in group A and fighter in group B that can be found.

Sample Test

Input
4
5 4
10 9 41 17 24
10 9 41 17 24
50 19 29 51
1 1
10
100
3 1
100 100 100
100
5 5
100 200 300 400 500
105 203 205 270 299
Output
Kasus #1: 2
Kasus #2: 90
Kasus #3: 0
Kasus #4: 1
#include<stdio.h>


int main(){
int lenA;
int lenB;

int tc,jml[100],powerA[100],powerB[100];
int Res;
int calc;

scanf("%d",&tc);


for(int z=0;z<tc;z++){

// Jumlah Pirate A
scanf("%d",&jml[0]);

// Jumlah Pirate B
scanf("%d",&jml[1]);


// Power tiap Pirate
for(int r=0;r<jml[0];r++){
    scanf("%d",&powerA[r]);

}
for(int h=0;h<jml[1];h++){
    scanf("%d",&powerB[h]);

}

lenA = jml[0];
lenB = jml[1];
Res = 1337;
for(int b=0;b<lenB;b++){
    for(int x=0;x<lenA;x++){
        if(powerA[x] > powerB[b]){
            calc = powerA[x]-powerB[b];
        }else{
            calc = powerB[b]-powerA[x];
        }
        
    if(Res == 1337){Res = calc; }else{  if(calc < Res) Res = calc;}
    }
}
    printf("Kasus #%d: %d\n",z+1,Res);
}
    

return 0;
}

18 Duel Maut

Comments