You are given
n
pairs of numbers. In every pair, the first number is always smaller than the second number. A pair(c,d)
can follow
A basic way to solving this problem would be to create a tree, where each node is a pair, and construct directed edges from one node to another when it is legal (ie, "A pair (c,d) can follow (a,b) if and only if b is less than c"). You can do a modified breadth first traversal from each node that keeps track of the length of the longest path from that node, and when you are finished with that can just look over all the nodes to find the longest path.