CF911E.Stack Sorting

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

假设你有一个数组 aa、一个栈 ss(初始为空)以及另一个数组 bb(也初始为空)。

你可以执行以下操作,直到 aa 和 ss 均为空:

  • 取出 aa 的首元素,将其压入栈 ss,并从 aa 中删除该元素(当 aa 非空时);
  • 取出栈 ss 的栈顶元素,将其追加到数组 bb 末尾,并从 ss 中弹出该元素(当 ss 非空时)。

上述操作可以以任意顺序执行。

若存在一种操作序列,使得最终数组 bb 按非降序排列,则称数组 aa 是栈可排序的(stack-sortable)。

例如,[3, 1, 2][3,\,1,\,2] 是栈可排序的,因为若执行如下操作,bb 将变为有序:

  1. 将 aa 的首元素 33 移除并压入 ss;
  2. 将 aa 的首元素 11 移除并压入 ss;
  3. 将 ss 的栈顶元素 11 弹出,并追加到 bb 末尾;
  4. 将 aa 的首元素 22 移除并压入 ss;
  5. 将 ss 的栈顶元素 22 弹出,并追加到 bb 末尾;
  6. 将 ss 的栈顶元素 33 弹出,并追加到 bb 末尾。

执行完所有操作后,b=[1, 2, 3]b = [1,\,2,\,3],因此 [3, 1, 2][3,\,1,\,2] 是栈可排序的。而 [2, 3, 1][2,\,3,\,1] 则不是栈可排序的。

现给定某个长度为 nn 的排列 pp 的前 kk 个元素(注:长度为 nn 的排列是指由 11 到 nn 的每个整数恰好出现一次组成的长度为 nn 的数组)。你需要补全该排列剩余的 n−kn - k 个元素,使得整个排列 pp 是栈可排序的。若存在多种方案,请选择字典序最大的排列 pp(数组 qq 字典序大于数组 pp 当且仅当存在某个整数 kk,使得对所有 i<ki < k 有 qi=piq_i = p_i,且 qk>pkq_k > p_k)。你不得交换或修改排列 pp 的前 kk 个元素。

请输出你能得到的字典序最大的排列 pp。

若不存在满足条件的排列,则输出 −1-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.

第一行包含两个整数 nn 和 kk(2≤n≤2000002 \leq n \leq 200000,1≤k<n1 \leq k < n)——分别为所求排列的长度,以及你被给定的元素个数。

第二行包含 kk 个整数 p1,p2,…,pkp_1, p_2, \dots, p_k(1≤pi≤n1 \leq p_i \leq n)——即排列 pp 的前 kk 个元素。这些整数两两互不相同。

输出格式

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.

如果可以恢复一个大小为 nn 的可栈排序排列 pp,使得 pp 的前 kk 个元素等于输入中给出的元素,则输出字典序最大的这样的排列。
否则输出 −1-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

输入解题思路,AI测评打分。不知道怎么写?

首页