找球号(一)

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
在某一国度里流行着一种游戏。游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i&lt;=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

set

常用操作:

1.元素插入:insert()

2.中序遍历:类似vector遍历(用迭代器)

3.反向遍历:利用反向迭代器reverse_iterator。

    例:

    set<int> s;

    ......

    set<int>::reverse_iterator rit;

    for(rit=s.rbegin();rit!=s.rend();rit++)

4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。

            set<int> s;

            s.erase(2);        //删除键值为2的元素

            s.clear();

5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。

            set<int> s;

            set<int>::iterator it;

            it=s.find(5);    //查找键值为5的元素

            if(it!=s.end())    //找到

                cout<<*it<<endl;

            else            //未找到

                cout<<"未找到";

6.自定义比较函数

    (1)元素不是结构体:

        例:

        //自定义比较函数myComp,重载“()”操作符

        struct myComp

        {

            bool operator()(const your_type &a,const your_type &b)

            [

                return a.data-b.data>0;

            }

        }

        set<int,myComp>s;

        ......

        set<int,myComp>::iterator it;

    (2)如果元素是结构体,可以直接将比较函数写在结构体内。

        例:

        struct Info

        {

            string name;

            float score;

            //重载“<”操作符,自定义排序规则

            bool operator < (const Info &a) const

            {

                //按score从大到小排列

                return a.score<score;

            }

        }

        set<Info> s;

        ......

        set<Info>::iterator it;

//转载

#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
int m,n,a;
set<int>s;
scanf("%d%d",&m,&n);
while(m--)
{
scanf("%d",&a);
s.insert(a);
}
while(n--)
{
scanf("%d",&a);
if(s.find(a)!=s.end())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

hash

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[10001000];
int main()
{
int m,n,x;
scanf("%d%d",&m,&n);
while(m--)
{
scanf("%d",&x);
a[x/32]=1<<x%32;
}
while(n--)
{
scanf("%d",&x);
if(a[x/32]&(1<<x%32))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

二分查找

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1000100];
int main()
{
int m,n;
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
scanf("%d",&a[i]);
sort(a,a+m);
while(n--)
{
int mid,k;
scanf("%d",&k);
int flog=0;
int l=0,r=m;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid]==k)
{
flog=1;
break;
}
else if(k<a[mid])
r=mid-1;
else
l=mid+1;
}
if(flog)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}


nyoj--86--找球号(一)(hash&&set&&二分)的更多相关文章

  1. nyoj 86 找球号(一)

    点击打开链接 找球号(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i ...

  2. nyoj 86 找球号(一)

    找球号(一) 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0& ...

  3. nyoj 86 找球号(一)(set,map)

    找球号(一) 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0& ...

  4. NYOJ 138 找球号(二) bitset 二进制的妙用

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i< ...

  5. NYOJ 138 找球号(二) (哈希)

    题目链接 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,还有一个空箱子,现在有两种动作:一种是&qu ...

  6. 简答哈希实现 (nyoj 138 找球号2)

    例题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=138 代码目的:复习哈希用 代码实现: #include "stdio.h&qu ...

  7. nyoj 138 找球号(二)(哈希)

    题目:nyoj——138 /*** 哈希求解...采用链表保存 插入时,可以去除重复 查找 找到该组,然后在改组的查找 当这个组不存在时或是没有找到时是 NO 其他是YES 1e6+1 时间最短 */ ...

  8. nyoj 528 找球号(三)(哈希)

    点解:题目链接 两种办法,1是使用容器set做 2必须知道这个结论,  突然感觉数论很强大啊,,,, /*//set容器处理 出一次加进去,再出现删掉,这个最后留下的就是那个只出现基数次的 #incl ...

  9. nyist oj 138 找球号(二)(hash 表+位运算)

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中.每一个球上都有一个整数编号i(0<=i< ...

  10. 找球号(一)(hask表)

    找球号(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i<= ...

随机推荐

  1. PSEUDO LEAST RECENTLY USED (PLRU) CACHE REPLACEMENT

    A multi-way cache system includes multi-way cache storage circuitry, a pseudo least recently used (P ...

  2. golang测试框架--smartystreets/goconvey

    视频教程和配套博客:goconvey - 课时 1:优雅的单元测试 Go 语言虽然自带单元测试功能,在 GoConvey 诞生之前也出现了许多第三方辅助库.但没有一个辅助库能够像 GoConvey 这 ...

  3. T1553 互斥的数 codevs

    http://codevs.cn/problem/1553/ 题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y ...

  4. CS Academy #32 G

    题意: 分析: 考虑如何求方案数 dp[i][j]表示i个数字的和为j的方案数,这是个经典问题,转移有两种,一个是填一个数字1,一个是整体加1 然后这个问题并不是求方案数,而是求对应的权值和 我们很容 ...

  5. ASP.NETCore使用AutoFac依赖注入

    原文:ASP.NETCore使用AutoFac依赖注入 实现代码 1.新建接口类:IRepository.cs,规范各个操作类的都有那些方法,方便管理. using System; using Sys ...

  6. IntelliJ IDEA cannot resolved 处理

    IntelliJ IDEA cannot resolved 处理 学习了:https://stackoverflow.com/questions/21577573/intellij-idea-can- ...

  7. 转:linux下ip修改与域名解析查看等

    转自: http://www.justwinit.cn/post/7038/ IP:     ifconfiggateway:172.16.0.254 [root@localhost ~]# nets ...

  8. 百度云分享文件自己设置password

    我们在用百度云分享的时候都是百度云随机给我们生成的password.我们能够通过以下的一条js脚本代码来自己定义百度云分享password javascript:require(["func ...

  9. odoo写邮件添加收件人

    在任何可以写消息的地方点击鼠标     或者回复消息     写消息的框会聚焦并变大             点击撰写框右上角的弹出窗图标     弹出完整的撰写消息窗口     在红色的地方添加收件 ...

  10. hdu 1250 Hat&#39;s Fibonacci

    pid=1250">点击此处就可以传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding ...