Educational Codeforces Round 35 (Rated for Div. 2)
题目链接:http://codeforces.com/contest/911/problem/E
E. Stack Sorting
time limit per test
memory limit per test
input
output
a, a stack s (initially empty) and an array b
a and s
- a, push it into s and remove it from a (if a
- s, append it to the end of array b and remove it from s (if s
You can perform these operations in arbitrary order.
b is sorted in non-descending order in the end, then array a is calledstack-sortable.
[3, 1, 2] is stack-sortable, because b
- 3 from a and push it into s;
- 1 from a and push it into s;
- 1 from s and append it to the end of b;
- 2 from a and push it into s;
- 2 from s and append it to the end of b;
- 3 from s and append it to the end of b.
b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
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.
p
-1.
Input
n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the first k elements of p. These integers are pairwise distinct.
Output
stack-sortable permutation p of size n such that the first k elements of p
-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
【题意】
大致可以理解为,你要构造一个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;
}
}