Advertisement blocking software were detected ;( Please add this webpage to whitelist.

BRIDGE - Building Bridges

The tribe soon discovers that just communication is not enough and wants to meet each other to form a joint force against the terminator. But there is a deep canyon that needs to crossed. Points have been identified on both sides on which bridge ends can be made. But before the construction could be started, a witch Chudael predicted that a bridge can only be built between corresponding end points, i.e. a bridge starting from the ith end point on one side can only end on the ith end point on the other side, where the position of end points is seen in the order in which the points were identified. If not, it would lead to the end of the tribe. The tribe just wants to make as many non-cutting bridges as possible, with the constraint in mind. Bridges "cut" if and only if they have exactly one common point that is not an end point.


The first line of the input contains test cases t (1<=t<=50). It is followed by 3*t lines, 3 for each test case. The first line of input for each test case contains the number of end points identified on each side, n (1<=n<=103). The second line contains x-coordinates of end points identified on the first side and similiarly the third line contains the x-coordinates of corresponding end points identified on the other side. The end points are inputted in the order in which they were identified. The x-coordinates can range between -103 to 103.


You are required to output a single line for each test case. The line contains a single integer – the maximum number of bridges possible with the constraints explained above.




2 5 8 10

6 4 1 2


5 3 10

6 4 1


1 2 3 4 5 6

3 4 5 6 1 2






Expalanation: For the first test case, two non-overlapping bridges can be formed between the 3rd and 4th end points on each side.

hide comments
e_coder: 2015-06-23 13:37:41

try this one:
1 2 2
1 1 1
ans: 3

Sudarshan K: 2015-05-31 14:03:06

No need to bang your head - O(n^2) passes :)

Madhav: 2015-04-19 12:53:32

Thankyou Shubham and Aparajita for the test cases!!

anando_du: 2015-03-04 19:11:55

same as BRDGHRD :v :v submitted same code . got AC . ^_^

aparajita choudhary: 2015-02-01 14:03:29

Test case for those who are getting WA-
0 2 1 1
1 4 4 3

Last edit: 2015-02-01 14:07:35
Kishlay Raj: 2014-12-30 04:12:39

thanks @shubham garg , really nice test case

Shubham Garg: 2014-12-19 18:31:49

try these test cases
0 1 1
1 2 -1
0 1 1
1 3 2


Last edit: 2014-12-19 18:32:28
Infinity: 2014-09-08 10:34:32

what is the worst case complexity

Rishabh Agrawal: 2014-09-04 16:17:33

If bridges cut at an end point, are they considered "cutting" or not.
As in
2 7
6 6
They cut at 6

||N0VICE||: 2014-09-04 05:23:15

finally AC
nice problem :)
take care of cases where bridges cut at an end point

Added by:Troika::Bytes
Time limit:0.193s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: ASMGCC AWK clang_c clang_cpp C++14 CLOJ COB dmd clang_d elixir F# fantom GO GROOVY nim NODEJS clang_objc objc PERL 6 pico PYPY PYTH 3.4 PYTH 3.2.3 n rust SCM chicken SED