#P2782. Maximum Non-Crossing Navigation Channels
Maximum Non-Crossing Navigation Channels
Maximum Non-Crossing Navigation Channels
There is a long river flowing from east to west with two straight banks: a north bank and a south bank. On each bank, there are N cities located at distinct positions. Each city on the north bank has exactly one friendly city on the south bank (with all friendly cities distinct). Every pair of friendly cities applies for a direct navigation channel (a straight line connecting the two cities) across the river. However, due to heavy fog, the government wants to avoid any two channels crossing to prevent accidents.
Your task is to help the government decide which channel applications to approve. In doing so, the government wants to approve as many applications as possible while ensuring that no two approved channels cross.
After sorting the pairs by the positions of the north bank cities, the condition for non-crossing channels becomes equivalent to selecting a subset of these pairs such that the sequence of the corresponding south bank positions is strictly increasing. In other words, you need to compute the length of the longest increasing subsequence (LIS) of the south bank positions. The LIS is defined as follows: \[ a_{i_1} < a_{i_2} < \cdots < a_{i_k} \] where k is maximized.
inputFormat
The input consists of three lines:
- An integer N indicating the number of cities on each bank.
- N space-separated integers, representing the positions of the cities on the north bank.
- N space-separated integers, representing the positions of the corresponding friendly cities on the south bank.
outputFormat
Output a single integer: the maximum number of channel applications that can be approved so that no two channels cross.
sample
3
1 3 2
3 2 1
2