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
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.
source : http://codeforces.com/problemset/problem/862/A
题解:
这道题的题意是给你n个数,再给你一个数m,问需要补充多少数,能使从0-m之间的数都连续,如果在这n个数中有某些数是在0-m中,则不需要补充这些数,如果在这n个数中,有一些数等于m,则需要进行一次操作将每一个数删掉,直到没有数和m相等,而每补充一个数则进行一次操作,最终输出总共需要多少次操作。
最需要注意的是某些特殊情况,例如1 3 3,因为m=3,所以需要将3剔除掉,再补充0,1,2,最后输出4个操作。
我们可以开两个数组,第一个数组用来储存这n个数,另一数用来记录哪些数出现过。
代码如下:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int a[109];
int b[109];
int main(int argc, char *argv[]) {
int n,m;
while(cin >> n >> m){
memset(a,0,sizeof(a));//初始化
memset(b,0,sizeof(b));//初始化
int num = 0;
for(int i=0; i<n; ++i){
cin >> a[i];
if(a[i] == m) num++;//剔除数
b[a[i]] += 1;
}
for(int i=0; i<m; ++i){
if(b[i] == 0) num += 1;//补充数
}
cout << num << endl;
}
}