UVa12210 - A Match Making Problem(排序)

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();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值