题目描述
AFei has many cards. Each card has a number written on it. Now he wants to takes some out of his card and puts them in a box. And he wants to know whether the card with the number x was in the box. So he has the following two operations:
0 x (It means to put a card with the number x in the box.)
1 x (It means to query if there is a card with the number x in the box.)
输入描述:
The first line of the input is an integer n (1 <= n <= 106), the number of operations. Next n lines represent n operations, and two integers k ( k∈{0,1}) and x (0<=x<=109) are separated by spaces on each line, as described above.
输出描述:
For each query, output one line “yes” if there is a card with the number x in the box, otherwise output one line “no”.
示例1
输入
5
0 1
1 2
0 2
1 3
1 2
输出
no
no
yes
PS:题意是0是插入元素,1是查询某个元素是否存在。 日常自闭,我一来用set做的,然后无限超时,后又换成二分查询,又无限WA。比赛完后看了大牛们的代码,感觉真的nb。又学到一手。
附5种方法花式AC:
#include<bits/stdc++.h>
using namespace std;
vector<bool> b(1000000009); //开个vecter存
int main(){
int t;
scanf("%d",&t);
while(t--){
int a;
int x;
scanf("%d",&a);
if(a){
scanf("%d",&x);
if(!b[x])
printf("no\n");
else
printf("yes\n");
}
else {
scanf("%d",&x);
b[x]=true;
}
}
return 0;
}
#include <ext/hash_map>
using namespace __gnu_cxx;
hash_map<int,int> map; //哈希map,我这个弱鸡不会
int main()
{
int n,x,y;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
if(x==0){
map[y]=1;
}else{
if(map[y])
puts("yes");
else
puts("no");
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int n,op,x;
unordered_map<int,int> mp;//不会
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&op,&x);
if(op)
{
if(mp.find(x)!=mp.end()) puts("yes");
else puts("no");
}
else mp.insert(make_pair(x,1));
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
bitset<1000000005> s;
int main() {
int n,k,x;
scanf("%d",&n);
while (n--) {
scanf("%d%d",&k,&x);
if (k==0) {
s.set(x);
} else {
if (s[x]==1)
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n,f,x;
unordered_set<int> s;
int main() {
scanf("%d",&n);
while(n--){
scanf("%d %d",&f,&x);
if(f == 0){
s.insert(x);
}else {
printf(s.count(x) == 1?"yes\n":"no\n");
}
}
return 0;
}
这些容器称为关联式容器具体:关联容器详解