CF911E.Stack Sorting

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Let's suppose you have an array aa , a stack ss (initially empty) and an array bb (also initially empty).

You may perform the following operations until both aa and ss are empty:

  • Take the first element of aa , push it into ss and remove it from aa (if aa is not empty);
  • Take the top element from ss , append it to the end of array bb and remove it from ss (if ss is not empty).

You can perform these operations in arbitrary order.

If there exists a way to perform the operations such that array bb is sorted in non-descending order in the end, then array aa is called stack-sortable.

For example, [3,1,2][3,1,2] is stack-sortable, because bb will be sorted if we perform the following operations:

  1. Remove 33 from aa and push it into ss ;
  2. Remove 11 from aa and push it into ss ;
  3. Remove 11 from ss and append it to the end of bb ;
  4. Remove 22 from aa and push it into ss ;
  5. Remove 22 from ss and append it to the end of bb ;
  6. Remove 33 from ss and append it to the end of bb .

After all these operations b=[1,2,3]b=[1,2,3] , so [3,1,2][3,1,2] is stack-sortable. [2,3,1][2,3,1] is not stack-sortable.

You are given kk first elements of some permutation pp of size nn (recall that a permutation of size nn is an array of size nn where each integer from 11 to nn occurs exactly once). You have to restore the remaining nkn-k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that pp is lexicographically maximal (an array qq is lexicographically greater than an array pp iff there exists some integer kk such that for every i<ki<k qi=piq_{i}=p_{i} , and qk>pkq_{k}>p_{k} ). You may not swap or change any of first kk elements of the permutation.

Print the lexicographically maximal permutation pp you can obtain.

If there exists no answer then output -1.

输入格式

The first line contains two integers nn and kk ( 2<=n<=2000002<=n<=200000 , 1<=k<n1<=k<n ) — the size of a desired permutation, and the number of elements you are given, respectively.

The second line contains kk integers p1p_{1} , p2p_{2} , ..., pkp_{k} ( 1<=pi<=n1<=p_{i}<=n ) — the first kk elements of pp . These integers are pairwise distinct.

输出格式

If it is possible to restore a stack-sortable permutation pp of size nn such that the first kk elements of pp are equal to elements given in the input, print lexicographically maximal such permutation.

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
    
首页