Kattis - missinggnomesD Missing Gnomes (思路题)

题目:

题意:

给出已经去除了几个数的一个序列,任务是将去除的数字插回去补全这个序列,输出字典序排在第一的那个补全的序列。

例如:

样例输入:

5 3

1 4 2

样例输出:

1 3 4 2 5

思路:

用数组存一下给出的序列,用一个队列存一下被去除的数字的序列(由小到大排列),然后分别比较两个容器的第一个元素,那个小就先输出哪一个,知道两个容器中的元素都输出完毕。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int a[maxn],vis[maxn];

int main(){
    //FRE();
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i<m; i++){
        scanf("%d",&a[i]);
        vis[a[i]] = 1;
    }
    queue<int> que;
    for(int i = 1; i<=n; i++){
        if(vis[i] == 0){
            que.push(i);
        }
    }
    int cnt = 0;
    while(cnt<m && !que.empty()){
        if(a[cnt] < que.front()){
            printf("%d\n",a[cnt++]);
        }
        else{
            printf("%d\n",que.front());
            que.pop();
        }
    }
    //将剩余的元素全部输出出来
    while(cnt<m){
        printf("%d\n",a[cnt++]);
    }
    while(!que.empty()){
        printf("%d\n",que.front());
        que.pop();
    }
    return 0;
}
/*
PutIn:
5 3
1
4
2
PutOut:
1
3
4
2
5
PutIn:
7 4
6
4
2
1
PutOut:
3
5
6
4
2
1
7
*/
View Code

 

转载于:https://www.cnblogs.com/sykline/p/9925777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值