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 theMEX 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.

【题意】:给你n个数(从0开始)和特定的MEX数,可以增加或者删除数。求在该数组中若让MEX为最小值,需要几次增加和删除操作。

【分析】:只要让0~x之间标记没有出现的数字补位,用计数器++即可。注意若给你的MEX数在数组中已经有,那么要删除原数组的已有数,ans要+1.

【代码】:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = ;
int n,m; int main()
{
cin>>n>>m;
int a[maxn];
int mp[maxn];
memset(mp,,sizeof(mp));
for(int i=;i<n;i++)
{
cin>>a[i];
mp[a[i]]=;
} int cnt=;
if(mp[m]==) cnt++;
for(int i=;i<m;i++)
{
if(mp[i]==)
cnt++;
}
cout<<cnt<<endl;
return ;
}

本人代码

#include <iostream>
using namespace std;
bool b[];
int main()
{
int n,x;
scanf("%d%d",&n,&x);
while (n--)
{
int a;
scanf("%d",&a);
b[a]=;
}
int ans=b[x];
for (int i=;i<x;i++)
ans+=!b[i];
printf("%d",ans);
}

官方题解

CF 862A Mahmoud and Ehab and the MEX【数组操作】的更多相关文章

  1. 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 ...

  2. CF 959E Mahmoud and Ehab and the xor-MST

    第一反应是打表找规律……(写了个prim)但是太菜了没找到 于是开始怀疑是不是我的表错了,又写了一个克鲁斯卡尔,然后结果是一样的……(捂脸) 后来从克鲁斯卡尔的算法上发现了一点东西,发现只有2的幂次长 ...

  3. 【Codeforces Round #435 (Div. 2) A】Mahmoud and Ehab and the MEX

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 让x没有出现,以及0..x-1都出现就可以了. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/std ...

  4. CF 1005B Delete from the Left 【模拟数组操作/正难则反】

    You are given two strings s and t. In a single move, you can choose any of two strings and delete th ...

  5. CF 959 E. Mahmoud and Ehab and the xor-MST

    E. Mahmoud and Ehab and the xor-MST https://codeforces.com/contest/959/problem/E 分析: 每个点x应该和x ^ lowb ...

  6. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  7. E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...

  8. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  9. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

随机推荐

  1. poj 2226 Muddy Fields (二分匹配)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7340   Accepted: 2715 Desc ...

  2. Codeforces Round #383 (Div. 1) C(二分图)

    一道很巧妙的二分图的题目 简单分析性质可知,一个合法序列一定是由12,21这样的子串构成的,所以相邻的每隔2个两两配对 然后BF和GF互相配对,思考一下,如果存在奇环,那么必定有一个BG有两个GF,或 ...

  3. 【题解】[国家集训队]Crash的数字表格 / JZPTAB

    求解\(\sum_{i = 1}^{n}\sum_{j = 1}^{m}lcm\left ( i,j \right )\). 有\(lcm\left ( i,j \right )=\frac{ij}{ ...

  4. Linux和Windows上实现的异同-Linux的自适应ACK

    上周有同事问,延迟ACK到底对应用层会产生什么后果,我也不知道该如何作答,于是丢了一个链接: TCP之Delay ACK在Linux和Windows上实现的异同-Linux的自适应ACK: 是的,这是 ...

  5. 【MySQL】数据库 --MySQL的安装

    本篇教程主要讲解在CentOS 6.5下编译安装MySQL 5.6.14! 1.卸载旧版本: 使用下面的命令检测是否安装有MySQL server <span style="font- ...

  6. jQuery源码分析笔记

    jquery-2.0.3.js版本源码分析 (function(){  (21,94) 定义了一些变量和函数 jQuery = function(){};  (96,283) 给JQ对象,添加一些方法 ...

  7. jQuery通过CSS()方法给指定的元素同时设置多个样式

    <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax ...

  8. bzoj3223 文艺平衡树 codevs3303 翻转区间

    splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...

  9. [bzoj1030][JSOI2007]文本生成器——AC自动机

    Brief Description 给定一些模式串,您需要求出满足以下要求的字符串的个数. 长度为m 包含任意一个模式串 Algorithm Design 以下内容来自神犇博客 首先运用补集转换,转而 ...

  10. bzoj 3190 维护栈

    我们可以将每一辆赛车看成一条直线,斜率为速度,纵截距为初始位置,那么问题就转化为求这n条直线处于最上面的直线.最上面是指在坐标系中,假设从x轴向下看,能看到的直线,只露一个点也算能看见.那么就类似水平 ...