zoukankan      html  css  js  c++  java
  • CF 862A Mahmoud and Ehab and the MEX【数组操作】

    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 = 105;
    int n,m;
    
    int main()
    {
        cin>>n>>m;
        int a[maxn];
        int mp[maxn];
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            mp[a[i]]=1;
        }
    
        int cnt=0;
        if(mp[m]==1) cnt++;
        for(int i=0;i<m;i++)
        {
            if(mp[i]==0)
                cnt++;
        }
        cout<<cnt<<endl;
        return 0;
    }
    本人代码
    #include <iostream>
    using namespace std;
    bool b[105];
    int main()
    {
        int n,x;
        scanf("%d%d",&n,&x);
        while (n--)
        {
            int a;
            scanf("%d",&a);
            b[a]=1;
        }
        int ans=b[x];
        for (int i=0;i<x;i++)
        ans+=!b[i];
        printf("%d",ans);
    }
    官方题解
  • 相关阅读:
    Linux 下安装nodejs
    Linux 下安装JDK
    ubuntu 把软件源修改为国内源
    vi/vim 命令使用详解
    不同浏览器css引入外部字体的方式
    npx 命令介绍
    ICloud没有密码怎么注销?
    装修后才知道的79件事
    天翼宽带政企网关B2-1P 如何获得超级管理员账号?
    家庭治疗偏头痛
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7569762.html
Copyright © 2011-2022 走看看