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
题目大概:
给出n个数,给出一个数x,看需要经过几步操作可以把x变为n个数中不包含的最小非负整数,操作有添加数和删除数。
思路:
像晒素数一样,把出现的比x小的数,晒出来,没出现的数就要添加,删除一般是当x出现在集合中时,删除x。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[102];
int b[102];
int main()
{
int n,x;
cin>>n>>x;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
int m=0;
for(int i=1;i<=n;i++)
{
if(a[i]<=x)b[a[i]]=1;
else break;
}
//cout<<b[x]<<endl;
for(int i=0;i<x;i++)
{
if(!b[i])m++;
}
if(b[x]==1)m++;
printf("%d\n",m);
return 0;
}