ACPC11B - Between the Mountains

no tags 

Did you try to ride a telepherique (cable car)? It is a lot of fun. Our company is trying to build a new telepherique line between two high mountains and you will be invited for a free ride. The trick to get your free ride is to help the company in building the telepherique line.

The company wants to build two platforms, one on each mountain. The line will extend between these two platforms. The suitable points for holding a platform in each mountain were determined, and the altitudes of these points were reported.

One of the talented engineers suggested that the company can save a lot of energy if the two stations have the same altitude or at least have altitudes which are as close to each other as possible. Your job is to select two points, one at each mountain, from those suitable points, so that the altitude difference between the two points is as little as possible.


Your program will be tested on one or more test cases. The first line of the input will be a single integer T , the number of test cases (1 ≤ T ≤ 100). Follows 2T lines containing the test cases, each on a pair of lines.

Each of the two lines in a case describe the altitudes of suitable points to build a platform on one mountain. Each line starts with an integer N , the number of reported altitudes (1 ≤ N ≤ 1,000), followed by N integers, which represent the altitudes reported from this mountain. Any altitude value will be between 1 and 1,000,000, inclusive.


For each test case, output, on a single line, a single number representing the minimum

altitude difference between two suitable platform points, one at each mountain.


8 1 3 5 7 9 7 3 1
8 2 4 6 8 10 8 6 2
8 2 3 5 10 9 3 2 1
7 1 2 6 12 13 3 2


hide comments
harry_shit: 2019-11-12 21:41:41

O(n*m) gives ac

Last edit: 2019-11-12 21:41:58
salman3007: 2018-10-19 03:53:39

merging of two arrays!!

masterchef2209: 2018-08-28 15:14:57

solved in O(nlog(n)) AC in 1 go :D

joe85123: 2018-07-21 09:32:27

after thinking for hours, I really needed some hint so I read that article from geeks for geeks provided by @srjsunny, and I couldn't help but felt a little frustrated.
I had the exactly same idea with that article at the very first place, but I wrote in C, which doesn't have the faster std::sort, so I used qsort instead and got TLE. Then I started to think for another solution other than bruteforce for HOURS but in vain :(

Last edit: 2018-07-21 09:32:38
sachinam_1397: 2018-05-31 22:01:16

can someone provide some test cases ????

sharif ullah: 2018-05-01 16:43:26

r u used binary search ? && gor WA???!
think about this
3 100 200 300
1 50

srjsunny: 2018-02-22 20:06:53

don't use brute force ,you won't learn anything rather see this

ameyanator: 2018-01-25 21:19:59

How about increasing the constraints to make this problem more interesting?

iambalu: 2017-10-14 14:57:46

sorry guys no need to think much!!!!!!!! just use nested loop test case is very weak it passes easily;

shubham9261: 2017-09-23 23:29:49

silly me :) learnt a silly thing,difference of min of two arrays is not the minimum difference ;)

Added by:mohamedwahba
Time limit:0.170s-0.823s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Arab Collegiate Programming Contest 2011