Codeforces 1364C

该篇博客介绍了一种C++算法,用于根据输入数组a构建新的数组b,其中b的每个元素是前i个元素中未出现过的最小整数。算法首先统计数组a中每个元素的出现次数,然后从小到大填充b。当遇到不同的元素时,直接添加到b中;如果遇到相同的元素,则从已排序的未出现过元素集合中取出并添加。
摘要由CSDN通过智能技术生成

题意:通过数组a构建数组b;其中ai是MEX(b1~bi);
思路:假如两个数字不同那么第i号元素一定是i-1号元素;
假如相同的话;只需要从小到大一直添加即可;

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int N = 200010;
typedef long long ll;
int a[N];
int vis[N];
inline int read() {
    int date = 0, w = 1; char c = 0;
    while (c < '0' || c > '9') {if (c == '-')w = -1; c = getchar();}
    while (c >= '0' && c <= '9') {date = date * 10 + c - '0'; c = getchar();}
    return date * w;
}
inline void out(int x) {
    if (x < 0) {putchar('-'); x = -x;}
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
#define x first
#define y second
int main() {
    int n = read();
    vector<int> v;
    vector<int>b;
    for (int i = 1; i <= n; i++) a[i] = read(), vis[a[i]]++;
    for (int i = n; i >= 0; i--) if (!vis[i]) v.push_back(i);
    a[0] = a[1];
    for (int i = 1; i <= n; i++) {
        if (a[i] != a[i - 1]) b.push_back(a[i - 1]);
        else b.push_back(v.back()), v.pop_back();
    }
    for (auto i : b) out(i), cout << " ";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值