Codeforces 862A Mahmoud and Ehab and the MEX

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/polanwind/article/details/78045141

题目链接: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;
}

猜你喜欢

转载自blog.csdn.net/polanwind/article/details/78045141
今日推荐