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 63 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.
what should be answer for this?
@Rohit Agarwal There is solution using BIT and using SegmentTrees.
Sorting and LIS(Longest increasing subsequence ) paves the way .. Just see the testcases and you will get the idea immediately ... And while finding LIS ,, for equal two points on second bridge , consider the first bridge coordinates too :p (I dont tell beyond this :p ) ,, and Expected complexity is O(n^2)..... Good Luck :D
Solved this problem with LIS but however the tag is binary search for this problem. In the beginning, I was trying to solve it with Binary Indexed tree but had some tc where it was printing wrong answers. Did anyone solved it using BS or BIT? Can someone please explain how to do it by that?
The knapsack-type DP solution I thought of initially gave TLE :( n^2 LIS solution passed though
I'm getting WA why?
@Shashank Tiwari: your post is very helpful, but actually answer of @Shubham Garg's test case is correct.
Let me explain the problem more clearly ....
try this one:
No need to bang your head - O(n^2) passes :)
|Cluster:||Cube (Intel G860)|
|Languages:||All except: ASM32-GCC MAWK GAWK BC C-CLANG CPP14-CLANG CPP14 CLOJURE COBOL COFFEE D-DMD D-CLANG DART ELIXIR FSHARP FANTOM FORTH GO GOSU GRV JS-MONKEY KTLN NIM NODEJS OBJC OBJC-CLANG OCT PERL6 PICO PROLOG PYPY PYTHON3 PY_NBC R RACKET RUST CHICKEN SED SQLITE SWIFT UNLAMBDA VB.NET|