#P6198. [EER1] 单调栈

[EER1] 单调栈

Description

A monotonic stack is a common data structure. If you have not learned it before, you can refer to the Monotonic Stack Tutorial to help understand the problem.

There is a permutation p0,p1,,pn1p_0, p_1, \cdots, p_{n-1} of length nn. Using this permutation, we generate a sequence SS of length nn, where SiS_i denotes the size of the increasing monotonic stack built from p0,p1,,pip_0, p_1, \cdots, p_i.

In other words, the sequence SS is generated by the following code:

stack<int> stk;
int n = p.size();
vector<int> S;
for (int i = 0; i < n; i++) {
  while (!stk.empty() && p[i] <= p[stk.top()]) stk.pop();
  stk.push(i);
  S.push_back((int)stk.size());
}

Now you are given part of the sequence SS. The parts that are not given may take any value. Please reconstruct a permutation pp that matches the given SS. If there are multiple possibilities, output the lexicographically smallest one. It is guaranteed that a solution exists.

Input Format

The first line contains an integer nn, the length of the permutation.

The next line contains nn integers, representing the sequence SS. Some entries are 1-1, meaning they can take any value.

Output Format

Output one line with nn integers, a permutation.

10
1 -1 2 3 -1 3 1 -1 2 3 

2 4 3 6 7 5 1 9 8 10

10
1 1 2 2 3 2 3 3 4 5 

2 1 5 4 6 3 8 7 9 10

Hint

Explanation for Sample #1: The output for Sample 11 corresponds to the sequence SS as 1 2 2 3 4 3 1 2 2 3, which matches the input. It can be proven that this is the lexicographically smallest permutation that can match the input.

For 100%100\% of the testdata, 1n1061 \leq n \leq 10^6.

This problem has 55 subtasks, and the limits are as follows:

Subtask 11 (55 points): It is guaranteed that 1n101 \leq n \leq 10.

Subtask 22 (1010 points): It is guaranteed that there is no 1-1 in the given SS.

Subtask 33 (2020 points): It is guaranteed that 1n3001 \leq n \leq 300.

Subtask 44 (2020 points): It is guaranteed that 1n30001 \leq n \leq 3000.

Subtask 55 (4545 points): No additional constraints.

Translated by ChatGPT 5