Codeforces 911E 简单模拟

题意:通过栈输出1到n的一组排列,要求字典序最大。题目给出了前k个数,若无解输出-1。

分析:模拟... 好像没啥好说的吧...


代码如下:

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;

const int maxn = 200000+10;
int n,k,cnt,mx;
int a[maxn];
stack<int>s;

bool judge(){
    cnt = 0;
    while (!s.empty()) s.pop();
    s.push(a[1]);
    for (int i=2; i<=k; i++){
        if (s.top()>a[i]) s.push(a[i]);
        else {
            while (!s.empty() && s.top()<a[i]){
               if (s.top()-cnt!=1) return 0;
               cnt++;
               s.pop();
            }
            s.push(a[i]);
        }
    }
    while (!s.empty() && s.top()-cnt==1 ) {
        cnt++;
        s.pop();
    }
    return 1;
}

inline int Max(int x, int y){return x>y?x:y; }
int main(){
    while (scanf("%d %d",&n,&k)==2){
        mx = -1;
        for (int i=1; i<=k; i++) {
            scanf("%d",&a[i]);
            mx = Max(mx,a[i]);
        }
        if (!judge()) printf("-1\n");
        else {
           while (!s.empty()) {
               int num = s.top();
               while (num>cnt+1) {
                   a[++k] = --num;
               }
               cnt = s.top();
               s.pop();
           }
           if (mx < n) {
               for (int i=n; i>=mx+1; i--) a[++k] = i;
           }
           for (int i=1; i<n; i++) printf("%d ",a[i]);
           printf("%d\n",a[n]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值