1. 程式人生 > >Stack Sorting CodeForces - 911E (思維+單調棧思想)

Stack Sorting CodeForces - 911E (思維+單調棧思想)

ive turn sce take color graph ima 就是 after

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 kelements 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


題意:給你一個數N和一個數 k , 然後是長度為K的數組,
讓你構造出一個N的全排列,使之前K項是給定的數組,並且滿足這個全排列是stack-sortable
題目給了stack-sortable的定義。

思路:
可以通過折耳根stack-sortab的性質和stack的性質來完成本題。
首先我們要知道這題的一個關鍵點,當一個數插入到棧的條件是這個數x小於棧中所以的數。
那麽我們首先對這K個數進行操作,對於每一個數p[i],先判斷能不能加到棧中(判斷條件是棧為空或者比棧頂小),
不能加入到棧中的就一定是符合條件的,那麽是直接輸出-1.
加入到棧中之後,進行彈出操作,從1開始用一個變量來維護彈出到的最大數,對於棧頂就是能彈出的就先從棧中彈出。
掃完後對剩余的棧中元素進行操作,剩余的棧中元素只所以沒有被彈出是因為肯定有some比它小的數在這K個中沒出現。
那麽我們就把(棧中元素之間)的數倒序分別輸出
然後再把前K個沒有的數進行倒序輸出即可。
因為要求字典序最大,所以是倒序輸出這些。

具體細節見accode
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,k;
int p[maxn];
int vis[maxn];

int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    cin>>n>>k;
    repd(i,1,k)
    {
        cin>>p[i];
        vis[p[i]]++;
    }
    stack<int> st;
    int m=inf;
    int isok=1;
    int now=0;
    repd(i,1,k)
    {
        if(p[i]+1==now)
        {
            now++;
        }else
        {

            if(st.empty())
            {
                st.push(p[i]);
            }else if(st.top()>p[i])
            {
                st.push(p[i]);
            }else
            {
                isok=0;
                break;
            }
            while(st.size()&&st.top()==now+1)
            {
                st.pop();
                now++;
            }

        }
    }
    if(!isok)
    {
        cout<<-1<<endl;
        return 0;
    }
    // while(!st.empty()&&st.size()!=1)
    // {
    //     st.pop();
    // }
    repd(i,1,k)
    {
        cout<<p[i]<<" ";
    }

    while(!st.empty())
    {
        for (int i = st.top()-1;i>=1; --i)
        {
            if(!vis[i])
            {
                cout<<i<<" ";
                vis[i]=1;
            }else
            {
                break;
            }
            /* code */
        }
        now=max(now,st.top());
        st.pop();
    }

    for(int i=n;i>now;i--)
    {
        cout<<i<<" ";
    }
    cout<<endl;

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch ==   || ch == \n);
    if (ch == -) {
        *p = -(getchar() - 0);
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 - ch + 0;
        }
    }
    else {
        *p = ch - 0;
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 + ch - 0;
        }
    }
}


Stack Sorting CodeForces - 911E (思維+單調棧思想)