#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 of length . Using this permutation, we generate a sequence of length , where denotes the size of the increasing monotonic stack built from .
In other words, the sequence 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 . The parts that are not given may take any value. Please reconstruct a permutation that matches the given . 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 , the length of the permutation.
The next line contains integers, representing the sequence . Some entries are , meaning they can take any value.
Output Format
Output one line with 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 corresponds to the sequence 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 of the testdata, .
This problem has subtasks, and the limits are as follows:
Subtask ( points): It is guaranteed that .
Subtask ( points): It is guaranteed that there is no in the given .
Subtask ( points): It is guaranteed that .
Subtask ( points): It is guaranteed that .
Subtask ( points): No additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号