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

【代码】:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int maxn = ;
  7. int n,m;
  8.  
  9. int main()
  10. {
  11. cin>>n>>m;
  12. int a[maxn];
  13. int mp[maxn];
  14. memset(mp,,sizeof(mp));
  15. for(int i=;i<n;i++)
  16. {
  17. cin>>a[i];
  18. mp[a[i]]=;
  19. }
  20.  
  21. int cnt=;
  22. if(mp[m]==) cnt++;
  23. for(int i=;i<m;i++)
  24. {
  25. if(mp[i]==)
  26. cnt++;
  27. }
  28. cout<<cnt<<endl;
  29. return ;
  30. }

本人代码

  1. #include <iostream>
  2. using namespace std;
  3. bool b[];
  4. int main()
  5. {
  6. int n,x;
  7. scanf("%d%d",&n,&x);
  8. while (n--)
  9. {
  10. int a;
  11. scanf("%d",&a);
  12. b[a]=;
  13. }
  14. int ans=b[x];
  15. for (int i=;i<x;i++)
  16. ans+=!b[i];
  17. printf("%d",ans);
  18. }

官方题解

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. java高精度类尝试

    java高精度尝试, poj2109,比较坑的题目 import java.io.*; import java.util.*; import java.math.*; public class Mai ...

  2. FFT多项式乘法模板

    有时间来补算法原理orz #include <iostream> #include <cstdio> #include <cmath> #include <c ...

  3. Netscaler的超高端口复用助力应对公网地址紧张

    Netscaler的超高端口复用助力应对公网地址紧张 http://blog.51cto.com/caojin/1898351 经常会有人问一个IP只有65535(姑且不考虑预留端口),从Big-ip ...

  4. [Leetcode] merge sorted array 合并数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  5. 阿里巴巴前端面试parseInt()函数的面试题

    JavaScript 是弱类型语言,为了保证数值的有效性,在处理数值的时候,我们可以对数值字符串进行强行转换.如 parseInt 取整和 parseFloat 取浮点数.Java 也有 Intege ...

  6. Math.abs为Integer.Min_VALUE返回错误的值

      Math.abs为Integer.Min_VALUE返回错误的值 这段代码: System.out.println(Math.abs(Integer.MIN_VALUE)); 回报-2147483 ...

  7. javascript中arguments的应用——不定项传参求和

    <script type="text/javascript"> window.onload=function(){ function sum(){ var result ...

  8. 数据结构&图论:K短路-可持久化可并堆

    本来A*就可以搞定的题,为了怕以后卡复杂度,找了个这么个方法 现阶段水平不够就不补充算法分析部分了 对于图G,建立一个以终点t为起点的最短路径构成的最短路径树 (就是反着跑一遍最短路,然后对于一个不为 ...

  9. [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流

    Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...

  10. CodeVS1747_NOI2002_荒岛野人_Savage_C++

    题目:http://codevs.cn/problem/1747/ 对于一个环,我们经常用取余来表示它走过若干圈后的位置 那么第 i 个野人第 x 年时所在的位置可表示为:(c[i]+p[i]*x)% ...