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);
    }
    官方题解
  • 相关阅读:
    Qt消息机制和事件、事件过滤
    QTableview 获取鼠标坐标的item(QModelIndex)
    qt 拖放dropEvent
    Qt获取控件位置,坐标总结
    Quick Easy FTP Server FTP工具文件传输使用
    Qt QDialog将窗体变为顶层窗体(activateWindow(); 和 raise() )
    makefile 通配符了解% $@ $^ $<
    QLocalServer和QLocalSocket单进程和进程通信
    RC4 加解密
    qt 拷贝文件设置进度条
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7569762.html
Copyright © 2011-2022 走看看