3705. 子集mex值

3705. 子集mex值

题目链接
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
const int N = 120;
int t;
int n;
int a[N];
int main() {
    cin >> t;
    while (t--) {
        cin >> n;
        int m;
        int sum = 0;
        int cnt = 0;
        memset(a, 0, sizeof(a));
        for (int i = 0; i < n; ++i) {
            scanf("%d", &m);
            a[m]++;
        }
        for (int i = 0; i <= 100; ++i) {
            if (cnt == 0) {
                if (a[i] == 0) {
                    sum += 2 * i;
                    break;
                } else if (a[i] == 1) {
                    sum += i;
                    cnt++;
                }
            } else if (cnt == 1) {
                if (a[i] == 0) {
                    sum += i;
                    break;
                }
            }
        }
        cout << sum << endl;
    }

    return 0;
}

题解:记录每个数字出现的次数,然后从小到大枚举,看看哪一个数字出现次数少于2,就特判一下但当前的状态。当cnt0的时候,就是一个集合的mex都没找到,这个时候有两种情况,一种情况是a[i]0,这个时候就是说两个集合都没有这个数字,所以sum直接加上这个数字的两倍并作为结果输出;一种情况是这个数字只出现了一个,所以就是一个集合没有这个数字,sum直接加上这个数字,然后cnt1。当cnt1的时候,只需要特判a[i]是否等于0,这样一定会出现两个集合都没有的数字,sum直接加上这个数字输出就行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值