您的位置:首页 > 其它

Codeforces 862A Mahmoud and Ehab and the MEX

2017-09-20 20:34 459 查看

题目链接:CF-862A

每日一水题,健康一辈子。
求一个含有n个非负整数的集合的mex,其中mex定义为该集合中没有出现的最小非负整数。其中有对集合有两种合法操作,一种是添加某个非负整数,一种是删除某个非负整数,问最小操作数可以使得集合的mex=x。
注意特判mex=0。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <string.h>

using namespace std;

int main()
{
int n, mex;
scanf("%d%d", &n, &mex);
bool temp[105];
memset(temp, false, sizeof(temp));
for (int i = 0;i < n;++i)
{
int v;
scanf("%d", &v);
temp[v] = true;
}
if (mex > 0)
{
int ans=0;
for (int i = 0;i < mex;++i)
{
if (!temp[i])
ans++;
}
if (temp[mex])
ans++;
printf("%d\n", ans);
}
else
{
if (temp[0])
printf("1\n");
else
printf("0\n");
}
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: