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?
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.
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
5 3 0 4 5 6 7
2
1 0 0
1
5 0 1 2 3 4 5
0
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.
题意:
就是给你n个数和你想要的数x,给你一个函数表示的是 这组数中没有出现的数的最小值,比如f(1,2,3,4)=0,
f(0,1, 4,5)=2,现在问你最少添加或者删除几个数可以得到你想要的数x 。
思路:水,桶拍一下,从0开始扫到x,没有出现的就++,坑点是 如果你n个数里有x的话,就要在++一下把x从n个数里删掉,才可以。。。就是因为这个坑,被hack掉了。。。
代码:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int flag[110];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int te;
int ans=0;
int p=0;
memset(flag,0,sizeof(flag));
for(int i=0;i<n;i++)
{
scanf("%d",&te);
flag[te]=1;
}
{
for(int i=0;i<m;i++)
{
if(!flag[i])ans++;
}
if(flag[m])ans++;
}
cout<<ans<<endl;
}
}