Match-making is a tough job and even then its long term success (A happy family) depends on two people who are often not involved in the match making process. But now, sites like facebook, twitter and communication devices/software like mobile phones, messengers have made the professional match makers jobless. So these angry and jobless match makers have gathered together to make the government pass a law in the parliament that will stop people from choosing their life partners. The law is stated below:
In a certain community, the most senior bachelor must marry a spinster (Female Bachelor) whose age is nearest to him. Then next senior bachelor will then marry a spinster whose age is nearest to him (of course if there is a tie, marrying anyone of them will do) excluding the spinster that has already got married. This process continues until there is no bachelor or spinster left. Of course a bachelor cannot marry two spinsters and a spinster cannot marry two bachelors. For example in a community there are four bachelors who are 21, 25, 26, 2 years old and four spinsters who are 26, 24, 25 and 35 years old. The diagram below shows the only possibility of marriage: (eg: The 26 year old bachelor marries the 25 years old spinster)
Now given the ages of the bachelors and the Spinsters in a community you will have to find the number of bachelors left, after all the marriages have taken place according to the law mentioned above. Also you have to report the age of the youngest bachelor left in the community if there is one.
Input
The input file contains at most 25 sets of inputs. The description of each set is given below:
The first line of each set contains two integers B (0<B<10000) and S (0<S<10000) which denotes the total number of bachelors and spinsters in the community respectively. Each of the next B lines contains one integer between 2 and 60 (inclusive) which denotes the age of one bachelor in the community. Each of the next S lines contains one integer between 2 and 60 (inclusive) which denotes the age of one spinster in the community. For simplicity you don’t need to worry about getting married at a very small age in this problem. That means unmarried people of all ages are valid bachelor or spinster.
Input is terminated by a line containing two zeroes.
Output
For each set of input produce one line of output. This line contains the serial of output followed by one or two integers. The first integer denotes the number of bachelors left in the community after all potential marriages have been completed. If this integer is not zero then print a second integer which denotes the age of the youngest bachelor left in the community after all possible marriages have been completed. Look at the output for sample input for details.
Sample Input Output for Sample Input
4 4 26 25 2 21 35 25 23 24 1 2 20 30 40 4 2 5 5 10 15 20 18 0 0 | Case 1: 0 Case 2: 0 Case 3: 2 5
|
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main
{
public static final boolean DEBUG = false;
public static final int N = 10010;
public BufferedReader cin;
public PrintWriter cout;
public StreamTokenizer tokenizer;
public int[] bachelor = new int[N];
public int[] spinster = new int[N];
public int b, s;
public int t = 1;
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(new FileInputStream
("e:\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
tokenizer = new StreamTokenizer(cin);
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tokenizer.nval);
} else return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
b = Integer.parseInt(next());
s = Integer.parseInt(next());
if (b == 0 && s == 0) return false;
for (int i = 0; i < b; i++) {
bachelor[i] = Integer.parseInt(next());
}
for (int i = 0; i < s; i++) spinster[i] = Integer.parseInt(next());
return true;
}
public void solve()
{
if (b <= s) {
cout.println("Case " + (t++) + ": 0");
cout.flush();
return;
}
Arrays.sort(bachelor, 0, b);
int min = bachelor[0];
cout.println("Case " + (t++) + ": " + (b - s) + " " + min);
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
while (solver.input()) {
solver.solve();
}
}
}