湖南大学第十五届程序设计竞赛——F(stl容器的使用)

题目描述
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;
}

这些容器称为关联式容器具体:关联容器详解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值