A11530 | Stack Sorting
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Let's suppose you have an array $a$ , a stack $s$ (initially empty) and an array $b$ (also initially empty).
You may perform the following operations until both $a$ and $s$ are empty:
- Take the first element of $a$ , push it into $s$ and remove it from $a$ (if $a$ is not empty);
- Take the top element from $s$ , append it to the end of array $b$ and remove it from $s$ (if $s$ is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array $b$ is sorted in non-descending order in the end, then array $a$ is called stack-sortable.
For example, $[3,1,2]$ is stack-sortable, because $b$ will be sorted if we perform the following operations:
1. Remove $3$ from $a$ and push it into $s$ ;
2. Remove $1$ from $a$ and push it into $s$ ;
3. Remove $1$ from $s$ and append it to the end of $b$ ;
4. Remove $2$ from $a$ and push it into $s$ ;
5. Remove $2$ from $s$ and append it to the end of $b$ ;
6. Remove $3$ from $s$ and append it to the end of $b$ .
After all these operations $b=[1,2,3]$ , so $[3,1,2]$ is stack-sortable. $[2,3,1]$ is not stack-sortable.
You are given $k$ first elements of some permutation $p$ of size $n$ (recall that a permutation of size $n$ is an array of size $n$ where each integer from $1$ to $n$ occurs exactly once). You have to restore the remaining $n-k$ elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that $p$ is lexicographically maximal (an array $q$ is lexicographically greater than an array $p$ iff there exists some integer $k$ such that for every $i<k$ $q_{i}=p_{i}$ , and $q_{k}>p_{k}$ ). You may not swap or change any of first $k$ elements of the permutation.
Print the lexicographically maximal permutation $p$ you can obtain.
If there exists no answer then output -1.
You may perform the following operations until both $a$ and $s$ are empty:
- Take the first element of $a$ , push it into $s$ and remove it from $a$ (if $a$ is not empty);
- Take the top element from $s$ , append it to the end of array $b$ and remove it from $s$ (if $s$ is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array $b$ is sorted in non-descending order in the end, then array $a$ is called stack-sortable.
For example, $[3,1,2]$ is stack-sortable, because $b$ will be sorted if we perform the following operations:
1. Remove $3$ from $a$ and push it into $s$ ;
2. Remove $1$ from $a$ and push it into $s$ ;
3. Remove $1$ from $s$ and append it to the end of $b$ ;
4. Remove $2$ from $a$ and push it into $s$ ;
5. Remove $2$ from $s$ and append it to the end of $b$ ;
6. Remove $3$ from $s$ and append it to the end of $b$ .
After all these operations $b=[1,2,3]$ , so $[3,1,2]$ is stack-sortable. $[2,3,1]$ is not stack-sortable.
You are given $k$ first elements of some permutation $p$ of size $n$ (recall that a permutation of size $n$ is an array of size $n$ where each integer from $1$ to $n$ occurs exactly once). You have to restore the remaining $n-k$ elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that $p$ is lexicographically maximal (an array $q$ is lexicographically greater than an array $p$ iff there exists some integer $k$ such that for every $i<k$ $q_{i}=p_{i}$ , and $q_{k}>p_{k}$ ). You may not swap or change any of first $k$ elements of the permutation.
Print the lexicographically maximal permutation $p$ you can obtain.
If there exists no answer then output -1.
输入格式
The first line contains two integers $n$ and $k$ ( $2<=n<=200000$ , $1<=k<n$ ) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains $k$ integers $p_{1}$ , $p_{2}$ , ..., $p_{k}$ ( $1<=p_{i}<=n$ ) — the first $k$ elements of $p$ . These integers are pairwise distinct.
The second line contains $k$ integers $p_{1}$ , $p_{2}$ , ..., $p_{k}$ ( $1<=p_{i}<=n$ ) — the first $k$ elements of $p$ . These integers are pairwise distinct.
输出格式
If it is possible to restore a stack-sortable permutation $p$ of size $n$ such that the first $k$ elements of $p$ are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
Otherwise print -1.
输入输出样例
输入 #1
5 3 3 2 1
输出 #1
3 2 1 5 4
输入 #2
5 3 2 3 1
输出 #2
-1
输入 #3
5 1 3
输出 #3
3 2 1 5 4
输入 #4
5 2 3 4
输出 #4
-1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted