codeforces A. Mahmoud and Ehab and the MEX

A. Mahmoud and Ehab and the MEX
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.

Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn’t exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .

Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?

Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.

The second line contains n distinct non-negative integers not exceeding 100 that represent the set.

Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.

Examples
inputCopy
5 3
0 4 5 6 7
outputCopy
2
inputCopy
1 0
0
outputCopy
1
inputCopy
5 0
1 2 3 4 5
outputCopy
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.

For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.

In the third test case the set is already evil.

题目大意:给定一个数列和一个数字 通过一系列操作让该数字成为该数列的邪恶数
方法:从零到该数字进行遍历 缺少一个数字需要操作一次 若该数字在原数列中存在 则需要额外多操作一次

#include<iostream>
#include<cstring>
using namespace std;
int num[110];
//核心思路,利用数组是否为空判断数字是否存在
int main() {
	int n,x,a;
	while(cin>>n>>x) {
		memset(num,0,sizeof(num));//初始化数组
		int i = n,ans=0;
		for(i = 0; i < n; i++) {
			cin>>a;
			num[a]++;// 存在的数字加1
		}
		for(i = 0; i < x; i++) {
			if(num[i]==0) {
				ans++;
			}
		}
		if(num[x]!=0)
			ans++;//若该数字在原数列中以及存在则操作数加一
		cout<<ans<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值