Increasing subsequence#832

Increasing subsequence (Editorial)

The problem asks to remove the smallest number of elements so that the remaining ones form a strictly increasing sequence. The remaining numbers form the longest increasing subsequence (LIS), so the problem reduces to finding and printing it.

We find the length of the LIS with the well-known O(n \log n) algorithm using the array and binary search (the function lower_bound). For each element x_i we additionally store — the position at which x_i was written into the array . The value L[i] + 1 equals the length of the longest increasing subsequence ending at the element x_i. The length of the whole LIS equals the final length of the array ; denote it by .

Restoring the answer. The array itself at the end of the process is, generally speaking, not an increasing subsequence of the original sequence, so we restore the answer using the array . We go through the elements from the end and look first for an element with L[i] = \mathit{len} - 1, then to the left of it for an element with L[i] = \mathit{len} - 2, and so on down to L[i] = 0. The found elements are written into the array from the end.

Why the result is increasing. Let the element x_i with L[i] = k be chosen, and let the next found element be x_j with L[j] = k - 1, that is, the nearest element to the left of with this value. When x_i was processed, it went to position , so the cell \mathit{lis}[k - 1] at that moment contained a number less than x_i. The last element to fill this cell before moment is exactly x_j, hence \mathit{lis}[k - 1] = x_j and x_j < x_i. Such an element x_j always exists: since x_i went to position , the cell k - 1 had already been filled by that moment.

Elements with the same value of form a non-increasing sequence: each next one replaces a number that is not smaller at its position in . Therefore, we must choose exactly the nearest element to the left with the required value of : elements further to the left with the same are not smaller than it and may turn out to be greater than x_i.

Complexity. Building the arrays and takes O(n \log n), and restoring the answer takes .

Example

Consider the sequence x = (2, 5, 3, 4, 6, 1). The figure shows the state of the array after processing each element. The cell into which the current element is written is highlighted: its index is exactly . If the element is appended to the end, the length of grows; otherwise it replaces a larger number.

We get L = (0, 1, 1, 2, 3, 0) and \mathit{len} = 4. Note that at the end \mathit{lis} = (1, 3, 4, 6), but this sequence is not a subsequence of : the number comes after the number in .

We restore the answer moving from the end. We look for L = 3: the element x_5 = 1 has L = 0 and is skipped, x_4 = 6 fits. Next we look for L = 2 — this is x_3 = 4, then L = 1 — this is x_2 = 3. The element x_1 = 5 also has L = 1, but the required value has already been found, and now we look for L = 0 — this is x_0 = 2.

Answer: numbers 2, 3, 4, 6. The numbers and are removed.

Algorithm Implementation

Declare the working arrays: is the input sequence, is the array for finding the LIS, is the position of the element x_i in the array , and is the restored answer. The variable stores the current length of the array .

#define MAX 100001
int x[MAX], L[MAX], lis[MAX], res[MAX];

Read the number of elements .

scanf("%d", &n);

Read the numbers one by one and process them immediately. The function lower_bound finds in the position of the first number not less than . Write there; if \mathit{pos} = \mathit{len}, the array becomes longer. Store L[i] = \mathit{pos}.

for (i = 0; i < n; i++)
{
  scanf("%d", &x[i]);
  int pos = lower_bound(lis, lis + len, x[i]) - lis;
  lis[pos] = x[i];
  if (pos == len) len++;
  L[i] = pos;
}

Restore the answer. Go through the elements from the end and look successively for elements with L = \mathit{len} - 1, \mathit{len} - 2, \ldots, 0. Each found element is written into , and is decreased.

int need = len - 1;
for (i = n - 1; i >= 0 && need >= 0; i--)
  if (L[i] == need) res[need--] = x[i];

Print the length of the LIS and the subsequence itself.

printf("%d\n", len);
for (i = 0; i < len; i++)
  printf("%d ", res[i]);
printf("\n");