Codeforces 862A Mahmoud and Ehab and the MEX

时间:2023-03-09 09:31:32
Codeforces 862A Mahmoud and Ehab and the MEX

传送门:CF-862A

A. Mahmoud and Ehab and the MEX
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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
input
5 3
0 4 5 6 7
output
2
input
1 0
0
output
1
input
5 0
1 2 3 4 5
output
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.

思路:先排序,然后求Mex。与x进行比较,如果mex=x,输出0;如果mex>x,直接去掉x,所以输出1;mex<x时,向数列中填数直到mex>x或者mex=0,输出操作的次数。需要注意的是,如果数位不足,mex的最大值依然小于x,需要额外判断。

 #include<iostream>
using namespace std; int main()
{
int k;
int re;
int rec;
int mex;
int temp;
int n,x;
int a[];
cin>>n>>x;
for (int i=; i<n; i++) cin>>a[i];
for (int i=; i<n; i++)
{
for (int j=i+; j<n; j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
} mex=a[n-]+;
re=n-;
for(int i=; i<n; i++)
{
if (a[i]!=i)
{
mex=i;
re=i-;
break;
}
} if (mex==x) cout<<;
else if (mex>x) cout<<;
else
{
rec=;
while(mex<x&&mex!=a[n-]+)
{
rec++;
int q=mex+;
re++;
k=re;
re=n-;
mex=a[n-]+;
for(int i=k;i<n;i++)
{
if(a[i]!=q)
{
re=i-;
mex=q;
break;
}
q++;
}
}
if(mex==x) cout<<rec;
else if(mex>x) cout<<rec+;
else cout<<(x-mex)+rec;
} return ;
}