cswork / OA打开练习工作台

AmazonOA #275

Maximum Domino Removals

估计困难可在线判题

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

The tile sizes are distinct, and removalOrder gives the original zero-based indices in the order they are removed. You may only remove a prefix of that order, and the remaining tiles keep their relative order. Find the maximum number of removals such that the longest strictly increasing subsequence of the remaining tiles has length at least minOrder. The original statement does not specify the output when there is no solution from the start; this site defines it as −1 and keeps such inputs. When minOrder=0, everything can be removed.

输入格式

The first line contains n minOrder, the second line contains tile, and the third line contains removalOrder. On this site 1≤n≤200000, 1≤tile[i]≤10^9 with all values distinct, and 0≤minOrder≤n; this site specifies that the removal order is a permutation of 0..n−1. Inputs whose initial LIS is shorter than minOrder are valid, and their answer is −1.

输出格式

Output a single integer, the answer.

样例 1

输入:

4 2
1 3 2 4
1 2 0 3

输出:

2

样例 2

输入:

3 2
3 2 1
0 1 2

输出:

-1

样例 3

输入:

1 0
9
0

输出:

1

样例说明

The answers are 2, −1 and 1 respectively. In the first example, after removing original indices 1 and 2, the remaining 1,4 still has length 2; removing 0 as well leaves only one item. In the second example the requirement already fails at the start, so 0 removals is not treated as feasible.