The North American Invitational Programming Contest 2018 D. Missing Gnomes

A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by a number 1..n1..n written on their hat.

Suppose there are 55 gnomes. The gnomes could line up like so: 1, 3, 4, 2, 51,3,4,2,5.

Now, an evil magician will remove some of the gnomes from the lineup and wipe your memory of the order of the gnomes. The result is a subsequence, perhaps like so: 1, 4, 21,4,2.

He then tells you that if you ordered all permutations of 1..n1..n in lexicographical order, the original sequence of gnomes is the first such permutation which contains the remaining subsequence. Your task is to find the original sequence of gnomes.

Input Format

Each input will consist of a single test case.

Note that your program may be run multiple times on different inputs.

Each test case will begin with a line with two integers nn and then m (1 \le m \le n \le 10^5)m(1mn105), where nn is the number of gnomes originally, and mm is the number of gnomes remaining after the evil magician pulls his trick. Each of the next mm lines will contain a single integer g (1 \le g \le n)g(1gn). These are the remaining gnomes, in order. The values of gg are guaranteed to be unique.

Output Format

Output nn lines, each containing a single integer, representing the first permutation of gnomes that could contain the remaining gnomes in order.

样例输入1

5 3
1
4
2

样例输出1

1
3
4
2
5

样例输入2

7 4
6
4
2
1

样例输出2

3
5
6
4
2
1
7

题目来源

The North American Invitational Programming Contest 2018

 

复制代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define  ll long long 
#define  N   100009
#define  gep(i,a,b)  for(int  i=a;i<=b;i++)
#define  gepp(i,a,b) for(int  i=a;i>=b;i--)
#define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
#define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
#define  mem(a,b)  memset(a,b,sizeof(a))
int  n,m;
bool vis[N];
int a[N];
int main()
{
    scanf("%d%d",&n,&m);
    int pos=1;
    int x;
    gep(i,1,m){
        scanf("%d",&a[i]);
        vis[a[i]]=1;
    }
    int j;
    gep(i,1,m){
        for(j=pos;j<=a[i];j++){
            if(!vis[j]){
                vis[j]=1;
                printf("%d\n",j);
            }
        }
        pos=j;
        printf("%d\n",a[i]);
    }
    gep(i,1,n){
        if(!vis[i]){
            printf("%d\n",i);
        }
    }
    return  0;
}
复制代码

 

posted on   cltt  阅读(148)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示