mex构造

Problem - C - Codeforces

 

 

 

a数组一定是递增的

set存下所有没出现的元素,然后遍历数组

如果a[i] != a[i - 1]
将 a[i-1]压进set(a[i]想出现要求a[i-1]出现过),输出set中最小的,在删除最小的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;;
const int N = 1e5 + 5;
int a[N];
int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    map<int, int>mp;
    set<int>s;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }
    for (int i = 0; i <= 2 * n; i++)
    {
        if (!mp[i])s.insert(i);
    }
    for (int i = 0; i < n; i++)
    {
        if (i!=0&&a[i] != a[i - 1])s.insert(a[i - 1]);
        cout << *s.begin() << ' ';
        s.erase(*s.begin());
    }
    return 0;
}

 

posted @ 2023-03-25 16:40  zhujio  阅读(14)  评论(0编辑  收藏  举报