This page is still under construction.

Parts of this page are still being built. What you see may change.

The Missing Permutation

Interview

Time limit15sMemory limit256 MB

Summary
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 PP of the numbers 11 through nn on a board. Every day he swaps a few numbers of PP to get a new permutation P′P', then looks for the longest increasing subsequence (LIS) of P′P'. 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 TT (1≤T≤1001 \le T \le 100).

Each test case starts with a line holding the length nn of the permutation (1≤n≤1051 \le n \le 10^5). The next line holds the incomplete permutation a1,a2,…,ana_1, a_2, \dots, a_n. If ai=0a_i = 0, the number that sat at position ii has fallen off.

The input is always valid. The nonzero values are distinct and lie between 11 and nn, and the fallen numbers are exactly the values of 11 through nn 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.

Examples3

  1. Example 1

    Input
    4
    5
    0 0 0 0 0
    10
    1 2 3 4 0 0 0 0 9 10
    14
    1 0 3 0 5 0 7 0 9 0 11 0 13 0
    9
    3 0 0 0 7 8 9 0 0
    
    Expected output
    5
    10
    14
    7
    
  2. Example 2

    Input
    2
    1
    0
    1
    1
    
    Expected output
    1
    1
    
  3. Example 3

    Input
    3
    5
    1 2 3 4 5
    5
    5 4 3 2 1
    8
    3 1 4 7 2 6 5 8
    
    Expected output
    5
    1
    4