AmazonOA #275
Maximum Domino Removals
Estimated HardJudge-ready
Topics (AI-labelled)Binary SearchSubsequences
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.
Input
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
Output a single integer, the answer.
Example 1
Input:
4 2
1 3 2 4
1 2 0 3
Output:
2
Example 2
Input:
3 2
3 2 1
0 1 2
Output:
-1
Example 3
Input:
1 0
9
0
Output:
1
Example notes
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.
Editorial
Loading solution…