子集mex值
AcWing 3705
https://www.acwing.com/problem/content/3708/
模拟,先记录每个数出现次数,然后由于要分成两个数组,依次遍历记录数组两遍,若出现次数大于0,则继续遍历,直到遇到出现次数为0.并且把遍历过的数的出现次数都减1,然后开始第二次遍历。
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int T,n;
int a[105];
int main()
{
cin >> T;
while (T--)
{
cin >> n;
memset(a, 0, sizeof a);//初始化
while (n -- )
{
int x;
cin >> x;
a[x] +=1 ;
}
int c = 0,d = 0;
for (int i = 0; i <= 100; i ++ )
{
if (a[i] > 0)
{
a[i] --;
}
else
{
c = i;
break;
}
}
for (int i = 0; i <= 100; i ++ )
{
if (a[i] > 0)
{
a[i] --;
}
else
{
d = i;
break;
}
}
cout << c + d << endl;
}
return 0;
}