• Codeforces 911E


    911E - Stack Sorting

    思路:

    用栈来模拟,能pop就pop,记下一个需要pop的数为temp,那么如果栈非空,栈顶肯定大于temp,那么加入栈 栈顶值-1 到 temp 的值,否则加入栈 n 到 temp 的值,如果需要加入的数之前已经出现过,答案则不存在。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pb push_back
    #define meme(a,b) memset(a,b,sizeof(a))
    
    const int N=2e5+5;
    bool vis[N];
    int a[N];
    stack<int>s;
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,k;
        cin>>n>>k;
        int temp=1;
        for(int i=1;i<=k;i++){
            cin>>a[i];
            vis[a[i]]=true;
            s.push(a[i]);
            while(s.size()&&s.top()==temp){
                s.pop();
                temp++;
            }
        }
        while(s.size()&&s.top()>temp||n+1>temp){
            int t;
            if(s.size())t=s.top();
            else t=n+1;
            for(int i=t-1;i>=temp;i--){
                if(vis[i]){
                    cout<<-1<<endl;
                    return 0;
                }
                else s.push(i),a[++k]=i,vis[i]=true;
            }
            while(s.size()&&s.top()==temp){
                s.pop();
                temp++;
            }
        }
        if(s.size())cout<<-1<<endl;
        else for(int i=1;i<=n;i++)cout<<a[i]<<' ';
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    JSON.parse()与JSON.stringify()的区别
    响应式布局
    document.selection
    jQuery $.proxy() 方法
    <转> 键值表
    jquery-jqzoom 插件 用例
    jquery 笔记
    前端表单验证常用的15个JS正则表达式<转>
    css 问题解决
    <转>break与continue
  • 原文地址:https://www.cnblogs.com/widsom/p/8352810.html
Copyright © 2020-2023  润新知