您的位置:首页 > 其它

Codeforces-911E:Stack Sorting(思维)

2017-12-30 22:58 295 查看
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:

Remove 3 from a and
push it into s;

Remove 1 from a and
push it into s;

Remove 1 from s and
append it to the end of b;

Remove 2 from a and
push it into s;

Remove 2 from s and
append it to the end of b;

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


#include<bits/stdc++.h>
using namespace std;
const int MAX=3e5;
int a[MAX],v[MAX];
stack<int>p;
int main()
{
int n,m,q=1;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
scanf("%d",&a[i]);
v[a[i]]=1;
p.push(a[i]);
while(!p.empty()&&p.top()==q)p.pop(),q++;//如果能按1,2,3,...的顺序输出,就不断出栈
}
vector<int>ans;
int pre=1;
while(!p.empty())//若此时栈里元素从栈顶到栈底依次增大才能满足题意,所以就要检查栈里相邻2个元素间是否有没入栈的
{
if(pre>p.top())
{
cout<<-1<<endl;
return 0;
}
for(int i=p.top();i>=pre;i--)//将当前栈顶元素和上一个栈顶元素之间没入栈的元素入栈,来满足栈从小到大的排列顺序
{
if(v[i]==0)ans.push_back(i),v[i]=1;
}
pre=p.top();
p.pop();
}
for(int i=n;i>=pre;i--)if(v[i]==0)ans.push_back(i);
for(int i=1;i<=m;i++)printf("%d ",a[i]);
for(int i=0;i<ans.size();i++)printf("%d ",ans[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: