Codeforces 911E Stack Sorting (模拟)

探讨如何根据部分已知序列构造一个可栈排序的全序列,并确保构造的序列字典序最大。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

E. Stack Sorting

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 qi = pi, and qk > pk). 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.

Input

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 p1, p2, ..., pk (1 ≤ pi ≤ n) — the first k elements of p. These integers are pairwise distinct.

Output

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.

Examples

input

5 3
3 2 1

output

3 2 1 5 4 

input

5 3
2 3 1

output

-1

input

5 1
3

output

3 2 1 5 4 

input

5 2
3 4

output

-1

 

 

题目链接:http://codeforces.com/contest/911/problem/E

 

题意:有n个数,其中k个数给定你入栈顺序(即数组a[]中存储的元素),剩余n-k个元素的输入顺序由你自己决定,使得n个元素的出栈顺序为升序。如果有多种答案,则输出入栈顺序字典序最大的一种。

 

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5+10;
int n,k,a[N],vis[N],top,b[N],num;

int main(){
    scanf("%d%d",&n,&k);
    for(int i = 0; i < k; i ++) scanf("%d",&a[i]);
    num = 1;
    for(int i = 0; i < k; i ++){
        vis[a[i]] = 1;
        b[++ top] = a[i];
        while(top && b[top] == num) top --, num ++;
    }
    int flag = 0;
    for(int i = 1; i < top; i ++){
        if(b[i] < b[i+1]){
            flag = 1; break;
        }
    }
    if(flag) printf("-1\n");
    else{
        for(int i = 0; i < k; i ++){
            if(i) printf(" ");
            printf("%d",a[i]);
        }
        b[top+1] = num-1;
        while(top > 0){
            for(int i = b[top]-1; i > b[top+1]; i --) printf(" %d",i);
            top --;
        }
        for(int i = n; i > b[top+1]; i --) printf(" %d",i);
        printf("\n");
    }
    return 0;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值