Codeforces 862A Mahmoud and Ehab and the MEX

来源:互联网 发布:mac air怎么截屏 编辑:程序博客网 时间:2024/06/05 05:06

题目链接: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");elseprintf("0\n");}//system("pause");return 0;}

原创粉丝点击