Girls and the Boxes

4

4 votes
Advanced Data Structures, Data Structures, Fenwick tree, Medium
Problem

You are given n boxes and each box contains two small boxes of Red and Blue color. The size of the Red and Blue colored boxes are bi and ri respectively.

Consider a sequence of distinct integers x1,x2,,xk (1xin).

Consider  x to be a suitable sequence if, for every 1i<k holds bxi<rxi+1 and rxi<bxi+1.

Determine the maximum possible size of the suitable sequence.

Input format

  • First line: n representing the number of available boxes (1n5105)
  • Next n lines: Two positive integers ri and bi (1ri, bi109)

Output format

Print only one integer representing the size of the maximum suitable sequence.

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

sequence [1,3] is suitable since b1<r3 and r1<b3. Its size is 2 and it can be shown that we can't arrange all 3 girls to form a suitable sequence of size 3.

Editor Image

?