E. Stack Sorting Codeforces(全排列與棧輔助排序)
Educational Codeforces Round 35 (Rated for Div. 2)
E. Stack Sorting time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputLet'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
- 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
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 kelements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
InputThe 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.
OutputIf 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 input5 3 3 2 1output
3 2 1 5 4input
5 3 2 3 1output
-1input
5 1 3output
3 2 1 5 4input
5 2 3 4output
-1【題意】
大致可以理解為,你要構造一個1~n的全排列。題目給出你前k項,你要把剩下的構造出來。
規定1、從構造好的序列中,只能從第一個數按順序取
2、有一個棧可以暫時儲存數字。
遵照兩種規定,每次往數列b中放一個數,使得最終數列b是升序的。
【分析】
通過多組示例分析可以得出一些規律。
從全排列開頭開始取數字,這個數字x有三種去向
1.如果數列b的下一項恰好是x,那就讓x加入b
2.不滿足1時,考慮讓x先入棧儲存,但是x必須小於棧中任意元素,才能入棧。
3.不滿足1,2時,這個全排列是構造不出來的。
模擬這個過程操作前k項,然後開始考慮棧中剩餘數字。
棧中之所以有剩餘是因為數列b中沒有比他們小的數,那就從棧頂開始考慮,把小於棧頂的數加入b中。
執行完這一過程後,倒序把所有沒加入的數字加入到b中即可。
【程式碼】
#include<bits/stdc++.h>
using namespace std;
int sta[202020];
int a[202020];
bool vis[202020];
int n,k;
int main()
{
while(cin>>n>>k)
{
memset(sta,0,sizeof(sta));
memset(vis,0,sizeof(vis));
int flag=1,b=0,top=0;
for(int i=1;i<=k;i++)
{
int x;cin>>x;
a[i]=x;
vis[x]=1;
if(x==b+1)//->b
b++;
else if(top==0||x<sta[top-1])
sta[top++]=x;
else flag=0;
while(top&&sta[top-1]==b+1)
{
b++;top--;
}
}
if(flag==0)
{
cout<<-1<<endl;
continue;
}
for(int i=1;i<=k;i++)
cout<<a[i]<<" ";
sta[top]=0;
for(int i=top-1;i>=0;i--)
{
for(int j=sta[i]-1;j>sta[i+1];j--)if(!vis[j])
{
cout<<j<<" ";
vis[j]=1;
}
}
for(int j=n;j;j--)if(!vis[j])
cout<<j<<" ";
cout<<endl;
}
}