ACM----CodeForces - Subset Mex
Given a set of integers (it can contain equal elements).
You have to split it into two subsets AA and BB (both of them can contain equal elements or be empty). You have to maximize the value of mex(A)+mex(B)mex(A)+mex(B).
Here mexmex of a set denotes the smallest non-negative integer that doesn't exist in the set. For example:
- mex({1,4,0,2,2,1})=3mex({1,4,0,2,2,1})=3
- mex({3,3,2,1,3,0,0})=4mex({3,3,2,1,3,0,0})=4
- mex(∅)=0mex(∅)=0 (mexmex for empty set)
The set is splitted into two subsets AA and BB if for any integer number xx the number of occurrences of xx into this set is equal to the sum of the number of occurrences of xx into AA and the number of occurrences of xx into BB.
Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤1001≤n≤100) — the size of the set.
The second line of each testcase contains nn integers a1,a2,…ana1,a2,…an (0≤ai≤1000≤ai≤100) — the numbers in the set.
Output
For each test case, print the maximum value of mex(A)+mex(B)mex(A)+mex(B).
Example
input
Copy
4 6 0 2 1 5 0 1 3 0 1 2 4 0 2 0 1 6 1 2 3 4 5 6
output
Copy
5 3 4 0
Note
In the first test case, A={0,1,2},B={0,1,5}A={0,1,2},B={0,1,5} is a possible choice.
In the second test case, A={0,1,2},B=∅A={0,1,2},B=∅ is a possible choice.
In the third test case, A={0,1,2},B={0}A={0,1,2},B={0} is a possible choice.
In the fourth test case, A={1,3,5},B={2,4,6}A={1,3,5},B={2,4,6} is a possible choice.
解题思路:将整个序列分为两个集合,首先构建集合A,从0开始按顺序在序列中查找(0,1,2,3........),若存在则放入集合A,当在序列中查找不到该值时,该值为mex(A)。同理构建集合B,将序列中剩下的元素放入集合B,计算mex(B),之后输出mex(A)+mex(B).
1 #include<stdio.h> 2 #include<string.h> 3 int main(){ 4 int t; 5 int num[150]; 6 scanf("%d",&t); 7 while(t--){ 8 int n; 9 int x; 10 int mA,mB; 11 memset(num,0,sizeof(num)); 12 scanf("%d",&n); 13 for(int i=0;i<n;i++){ 14 scanf("%d",&x); 15 num[x]++; 16 } 17 for(int i=0;i<=101;i++){//集合A 18 if(num[i]>=1) 19 num[i]--; 20 else{ 21 mA=i;break; 22 } 23 24 } 25 for(int i=0;i<=101;i++){ 26 if(num[i]>=1) 27 num[i]--; 28 else { 29 mB=i;break; 30 } 31 } 32 33 printf("%d\n",mA+mB); 34 } 35 }