NYOJ86找球号(一)

找球号(一)

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述
在某一国度里流行着一种游戏。游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,现在说一个随机整数k(0<=k<=100000100),判断编号为k的球是否在这堆球中(存在为"YES",否则为"NO"),先答出者为胜。现在有一个人想玩玩这个游戏,但他又很懒。他希望你能帮助他取得胜利。
输入
第一行有两个整数m,n(0<=n<=100000,0<=m<=1000000);m表示这堆球里有m个球,n表示这个游戏进行n次。
接下来输入m+n个整数,前m个分别表示这m个球的编号i,后n个分别表示每次游戏中的随机整数k
输出
输出"YES"或"NO"
样例输入
6 4
23 34 46 768 343 343
2 4 23 343
样例输出
NO
NO
YES
YES
来源
[苗栋栋]原创
上传者
苗栋栋

STL map


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#define mes(x) memset(x, 0, sizeof(x))
typedef long long ll;
#define Pii pair<int, int>
#define Pll pair<ll, ll
#define INF 1e9+7
using namespace std;
int main()
{
    //freopen("/home/ostreambaba/桌面/input.txt", "r", stdin);
    //mes(M);
    int m, n, w;
    scanf("%d %d", &m, &n);
    map<int, int> mp;
    for(int i = 0; i < m; ++i)
    {
        scanf("%d", &w);
        ++mp[w];
    }
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &w);
        cout << (mp[w]?"YES":"NO") << endl;
    }
    return 0;
}

STL set的做法

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#define mes(x) memset(x, 0, sizeof(x))
typedef long long ll;
#define Pii pair<int, int>
#define Pll pair<ll, ll
#define INF 1e9+7
using namespace std;
int main()
{
    //freopen("/home/ostreambaba/桌面/input.txt", "r", stdin);
    //mes(M);
    int m, n, w;
    scanf("%d %d", &m, &n);
    set<int> st;
    for(int i = 0; i < m; ++i)
    {
        scanf("%d", &w);
        st.insert(w);
    }
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &w);
        cout << (st.find(w)!=st.end()?"YES":"NO") << endl;
    }
    return 0;
}



sort+binary_search

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#define mes(x) memset(x, 0, sizeof(x))
typedef long long ll;
#define Pii pair<int, int>
#define Pll pair<ll, ll
#define INF 1e9+7
int M[1000005];
using namespace std;
int main()
{
   // freopen("/home/ostreambaba/桌面/input.txt", "r", stdin);
    int m, n, w;
    scanf("%d %d", &m, &n);
    for(int i = 0; i < m; ++i)
    {
        scanf("%d", &M[i]);
    }
    sort(M, M+m);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d", &w);
        cout << (binary_search(M, M+m, w)?"YES":"NO") << endl;
    }
    return 0;
}

最优代码mark一下,用的hash

 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int hash[3125100],n,m;
int main()
{
   // freopen("in.txt","r",stdin);
    scanf("%d %d",&n,&m);
//    memset(hash,0,sizeof(hash));
    int x,t,mod;
    char ch;
    for(int i=0; i<n; i++)
    {
        // scanf("%d",&x);
        x=0;
        while(!((ch=getchar())>='0'&&ch<='9'))
        {
        }
        x=(ch-'0');
        while((ch=getchar())>='0' && ch<='9')
            x=x*10+(ch-'0');
        t=x>>5;
        mod=x-(t<<5);
        hash[t]|=(1<<mod);
    }
    for(int i=0; i<m; i++)
    {
//            scanf("%d",&x);
        x=0;
        while(!((ch=getchar())>='0'&&ch<='9'))
        {
        }
        x=(ch-'0');
        while((ch=getchar())>='0' && ch<='9')
            x=x*10+(ch-'0');
        t=x>>5;
        mod=x-(t<<5);
        if(hash[t]&(1<<mod))
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
        


  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值