cswork / OA打开练习工作台

AmazonOA #57

Get Max Matching (Match the Column)

估计中等可在线判题

考点(AI 标注)子序列二分查找

The i-th line connects i to match[i]. Two lines cross only when their index order is the reverse of their target order; lines with the same target do not count as crossing. Find the maximum number of pairwise non-crossing lines that can be kept.

输入格式

The first line contains n (1..100000), and the second line contains n match values (1..n).

输出格式

Output a single integer answer.

样例 1

输入:

6
3 1 4 2 3 5

输出:

4

样例 2

输入:

3
1 1 1

输出:

3

样例 3

输入:

3
3 2 1

输出:

1

样例说明

Example 1: keep the four lines with targets 1, 2, 3, 5. Example 2: all targets are equal, which does not meet the strictly-reversed definition of crossing, so all three lines can be kept. Example 3: the targets are strictly decreasing and every two lines cross, so only 1 is kept.