WC '17 Finals S1 - An Interspecific Army

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.4s
Memory limit: 64M

Author:
Problem type
Woburn Challenge 2017-18 Finals Round - Senior Division

The Great Cow-Monkey War of 2017 has left Scarberia in a fragile and unstable state. Interspecies tension is at an all-time high, and both sides are working relentlessly to rebuild their economies, social systems, and militias. Little do the cows and monkeys know that amidst the turmoil, the world is at risk of being taken over by an even more dangerous foe — a group of aliens known as the Party of Extraterrestrial Gangsters (or PEG for short)! PEG has decided that it's the perfect time to divide and conquer Scarberia by exploiting its current internal hostilities. Fortunately, Bo Vine and the Head-Monkey have learned of the impending invasion in time, and an important realization has dawned on them: now is not the time to engage in petty quarrels with one another. All of Scarberia must unite, at least temporarily, to face an even greater evil. The animals have thus formed a truce, and started to plan for the upcoming war!

With an overall battle formation decided, the cows and monkeys are now ready to fight against the incoming alien invaders! …Well, almost. You see, the commanders realized that it is not too effective to simply line up large platoons of uniformly cows or uniformly monkeys. They must work together at a more local scale, with each cow and monkey taking immediate advantage of each others' strengths and weaknesses. As such, the commanders have decided to pair up cows and monkeys to form individual combat units on the battlefield.

Between the united armies, there are N cows and N monkeys (1 \le N \le 100\,000). The i-th cow has a combat skill level of C_i (1 \le C_i \le 10^6), while the i-th monkey has a combat skill level of M_i (1 \le M_i \le 10^6). Each cow is to be paired up with a monkey to form a combat unit, such that there are N combat units total and each of the 2N animals is a part of exactly one unit.

A combat unit is most effective when the skill levels of its two members don't differ too greatly. More formally, we can define the combat skill differential of a given unit to be the absolute difference between the combat skill levels of the two animals making up that unit. For a given pairing of the 2N animals, let D denote the maximum combat skill differential across all N combat units. Bo Vine and the Head-Monkey would like to pair up the animals such that D ends up being as small as possible. Can you help them determine the minimum possible value of D which can be achieved?

Subtasks

In test cases worth 2/9 of the points, N \le 10 and 1 \le C_i, M_i \le 2 for each i.
In test cases worth another 2/9 of the points, N \le 1\,000 and 1 \le C_i, M_i \le 2 for each i.
In test cases worth another 4/9 of the points, N \le 1\,000.

Input Specification

The first line of input consists of a single integer, N.
The second line consists of N space-separated integers, C_1, \dots, C_N.
The third line consists of N space-separated integers, M_1, \dots, M_N.

Output Specification

Output a single integer, the minimum possible value of D which can be achieved.

Sample Input

4
5 1 9 1
2 7 4 6

Sample Output

3

Sample Explanation

One possible optimal arrangement is to pair:

  • cow 1 with monkey 2 (a combat skill differential of |5 - 7| = 2)
  • cow 2 with monkey 1 (a combat skill differential of |1 - 2| = 1)
  • cow 3 with monkey 4 (a combat skill differential of |9 - 6| = 3)
  • cow 4 with monkey 3 (a combat skill differential of |1 - 4| = 3)

The maximum of these differentials is 3, and it is not possible to go any lower.


Comments