The Missing Permutation
InterviewTime limit15sMemory limit256 MB
Fill the zeros with the missing values to maximize the length of the longest increasing subsequence.
- Level
Medium5 of 10
- Topics
- Greedy, Dynamic programming, Binary search
- Solved
- No attempts yet
Problem
Little Tomato keeps a permutation of the numbers through on a board. Every day he swaps a few numbers of to get a new permutation , then looks for the longest increasing subsequence (LIS) of . He believes that he will eventually find a faster way to compute an LIS.
One day an earthquake shook some of the numbers off the board. Every fallen number goes back into an empty slot, and Tomato picks which slot each one lands in. How long can the LIS of the restored permutation be?
An increasing subsequence is a subsequence whose values grow from left to right.
Input
The first line contains the number of test cases ().
Each test case starts with a line holding the length of the permutation (). The next line holds the incomplete permutation . If , the number that sat at position has fallen off.
The input is always valid. The nonzero values are distinct and lie between and , and the fallen numbers are exactly the values of through that do not appear. The whole input is smaller than 10MB.
Output
For each test case, print the largest possible length of the LIS of the restored permutation on its own line.