• 1230 元素查找


    1230 元素查找

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 钻石 Diamond
     
     
     
    题目描述 Description

    给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。

    输入描述 Input Description

    第一行两个整数 n 和m。

    第二行n个正整数(1<=n<= 100000)

    第三行m个整数(1<=m<=100000)

    输出描述 Output Description

    一共m行,若出现则输出YES,否则输出NO

    样例输入 Sample Input

    4 2

    2 1 3 4

    1 9

    样例输出 Sample Output

    YES

    NO

    数据范围及提示 Data Size & Hint

    所有数据都不超过10^8

    分类标签 Tags 点此展开 

     
    题解:
    题目数据没有负数,直接查就好了
    AC代码1:
    #include<cstdio>
    bool a[100000001];
    int main(){
        int n,m,t;
        scanf("%d%d",&n,&m);
        while(n--){
            scanf("%d",&t);
            a[t]=1;
        }
        while(m--){
            scanf("%d",&t);
            printf("%s
    ",a[t]?"YES":"NO");
        }
        return 0;
    }

    AC代码2:

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define N 100100
    int n,m,a[N],b[N];
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) scanf("%d",a+i);    
        for(int i=0;i<m;i++) scanf("%d",b+i);
        sort(a,a+n);
        for(int i=0;i<m;i++){
            int p=lower_bound(a,a+n,b[i])-a;
            printf("%s
    ",a[p]==b[i]?"YES":"NO");
        }
        return 0;
    }

    AC代码3:

    #include<cstdio>
    #include<set>
    using namespace std;
    inline void read(int &x){
        register char ch=getchar();x=0;
        while(ch>'9'||ch<'0') ch=getchar();
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    }
    set<int>s;
    set<int>::iterator it;
    int main(){
        int n,m,a;
        read(n);read(m);
        for(int i=1;i<=n;i++) read(a),s.insert(a);
        for(int i=1;i<=m;i++) read(a),it=s.find(a),puts(it!=s.end()?"YES":"NO");
        return 0;
    }
  • 相关阅读:
    python file op
    python write read
    Linux MD RAID 10
    bitmap.h
    1
    write 1 to block device
    tr '00' '377' < /dev/zero | dd of=/dev/$i bs=1024 count=1024000
    Superblock
    echo -e "33[41;36m something here 33[0m"
    May It Be
  • 原文地址:https://www.cnblogs.com/shenben/p/5878795.html
Copyright © 2020-2023  润新知