0
点赞
收藏
分享

微信扫一扫

A. Subset Mex(贪心算法+构造)Codeforces Round #670 (Div. 2)

A. Subset Mex(贪心算法+构造)Codeforces Round #670 (Div. 2)_算法
测试样例

input
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
5
3
4
0

Note

In the first test case, A={0,1,2},B={0,1,5} is a possible choice.

In the second test case, A={0,1,2},B=∅ is a possible choice.

In the third test case, A={0,1,2},B={0} is a possible choice.

In the fourth test case, A={1,3,5},B={2,4,6} is a possible choice.

题意: 给你一个整数序列,你可以将这个序列分成两个集合A和B,分成集合的条件为它们必须要有相同的元素或者其中一个为空集。现定义一种运算A. Subset Mex(贪心算法+构造)Codeforces Round #670 (Div. 2)_贪心_02:其值为A中未出现的最小非负整数。

解题思路: 很显然这道题是一道贪心的算法题,由于我们的mex运算是选择未出现的最小非负整数,**那么我们固然是想构建一个从0到最大的集合,这样,它就可以选取一个最大的数了。这就是我们贪心的思路。**所以我们需要先构建一个集合。(构建的思路就是统计每个数的出现次数,若加入集合,则次数减1.)OK,具体看代码。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e2+3;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int cnt[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
memset(cnt,0,sizeof(cnt));
int temp;
rep(i,0,n-1){
cin>>temp;
cnt[temp]++;
}
int ans=0;
rep(i,0,101){
if(cnt[i]>=1){
cnt[i]--;
}
else{
ans+=i;
break;
}
}
rep(i,0,101){
if(cnt[i]>=1){
}
else{
ans+=i;
break;
}
}
cout<<ans<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论