部分转自:
找球号(一)
时间限制: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
- 第一行有两个整数m,n(0<=n<=100000,0<=m<=1000000);m表示这堆球里有m个球,n表示这个游戏进行n次。
我的set代码:set_lian.cpp
#include <iostream>
#include <set>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
set<int>si;
int i=0;
while(i<m)
{
int t;
cin>>t;
si.insert(t);
i++;
}
i=0;
while(i<n)
{
int t;
cin>>t;
if( si.count(t) )
{
cout<<"YES"<<endl;
}else
{
cout<<"NO"<<endl;
}
i++;
}
}
运行:(数据在set_test)
chen@chen-book1:~$ g++ set_lian.cpp -o set_lian
chen@chen-book1:~$ ./set_lian <set_test
NO
NO
YES
YES
原文的两种代码:
#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
int main()
{
int x,n,m;
set<int> s;
cin>>n>>m;
for(int i=0;i<n;i++)
{scanf("%d",&x); s.insert(x);}
for(int i=0;i<m;i++)
{
scanf("%d",&x);
if(s.find(x)!=s.end())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
chen@chen-book1:~$ g++ set_lian2.cpp -o set_lian
chen@chen-book1:~$ time ./set_lian <set_test
YES
YES
YES
YES
real 0m0.004s
user 0m0.000s
sys 0m0.000s
#include <stdio.h>
#define MAXN 3125010
int vis[MAXN] = {0} ;
int main()
{
int m , n , x ;
int i ;
scanf("%d%d", &m , &n ) ;
for( i = 0 ; i < m ; ++i )
{
scanf("%d", &x ) ;
vis[ x / 32 ] |= 1 << x % 32 ;
}
for( i = 0 ; i < n ; ++i )
{
scanf("%d", &x ) ;
if( vis[ x / 32 ] & ( 1 << x % 32 ) )
printf("YES\n");
else
printf("NO\n");
}
return 0 ;
}
运行:
chen@chen-book1:~$ g++ set_lian3.cpp -o set_lian
chen@chen-book1:~$ time ./set_lian <set_test
NO
NO
YES
YES
real 0m0.001s
user 0m0.000s
sys 0m0.000s
chen@chen-book1:~$