【wikioi】1230 元素查找(巨水题+set/hash)
http://wikioi.com/problem/1230/
这题我真的不好意思写题解了。。。set练手。。
#include <cstdio>
#include <set>
using namespace std;
int main() {
int n, m, t;
scanf("%d%d", &n, &m);
set<int> s;
for(int i=1; i<=n; ++i) scanf("%d", &t), s.insert(t);
for(int i=1; i<=m; ++i) scanf("%d", &t), s.count(t)?puts("YES"):puts("NO");
return 0;
}
还是写第二种方法吧,hash。(速度比第一种快)
(我从来没写过啊囧,但是一看就会了,太简单了。。)
我们一般是用mod来做hash的,但是有时会有冲突,肿莫办!!!!!
哈哈,很简单,如果有冲突我就往后跑,往后占!!!查找也是一样,不等于就往后跑,往后占!!
这样,hash的空间就可以压很小了。(但是注意,如果范围很密集,这样做会失效。。。)
#include <cstdio>
#include <set>
using namespace std;
int a[100009], md=100007;
void ins(const int &x) { for(int k=x%md; a[k]!=x; k=(k+1)%md) if(!a[k]) { a[k]=x; return; } }
int sea(const int &x) { for(int k=x%md; a[k]!=x; k=(k+1)%md) if(!a[k]) return 0; return 1; }
int main() {
int n, m, t;
scanf("%d%d", &n, &m);
set<int> s;
for(int i=1; i<=n; ++i) scanf("%d", &t), ins(t);
for(int i=1; i<=m; ++i) scanf("%d", &t), sea(t)?puts("YES"):puts("NO");
return 0;
}
题目描述 Description
给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。
输入描述 Input Description
第一行两个整数 n 和m。
第二行n个正整数(1<=n<= 100000)
第三行m个整数(1<=m<=100000)
输出描述 Output Description
一共m行,若出现则输出YES,否则输出NO
样例输入 Sample Input
4 2
2 1 3 4
1 9
样例输出 Sample Output
YES
NO
数据范围及提示 Data Size & Hint
所有数据都不超过10^8
【wikioi】1230 元素查找(巨水题+set/hash)的更多相关文章
- [Codevs 1230]元素查找(手写哈希表)
题目连接:http://codevs.cn/problem/1230/ 说白了就是要我们自己手写一个哈希表的数据结构来实现加入和查找功能.map也能直接过(我第一次写就是用map骗AC的) 提一下个人 ...
- 【BZOJ】2761: [JLOI2011]不重复数字(set+巨水题+超坑题)
http://www.lydsy.com/JudgeOnline/problem.php?id=2761 太水了,不说了. 但是这格式错误我已经没话说了....行末不能有空格 #include < ...
- codevs 1230 元素查找
题目链接:http://codevs.cn/problem/1230/ 题解: 会有很多方法写这道题,写个裸的哈希练练手 #include<cstdio> ,MOD=; int n,m,h ...
- CODE[VS] 1230 元素查找
1.题目戳这里 2.代码: #include<iostream> #include<algorithm> using namespace std; int n,m,a[1000 ...
- 二分--LIGHTOJ 1088查找区间(水题)
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const ...
- codevs——1230 元素查找
时间限制: 1 s 空间限制: 128000 Ks 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数 ...
- AC日记——元素查找 codevs 1230
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有 ...
- 元素查找(codevs 1230)
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出n个正整数,然后有m个询问,每 ...
- codevs1230元素查找(hash)
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个 ...
随机推荐
- centos7 安装kvm, 并创建虚拟机
# yum –y install qemu-kvm qemu-img bridge-utils # yum –y install libvirt virt-install virt-manager # ...
- ubuntu搭建lamp环境
首先安装软件: sudo apt-get install apache2 sudo apt-get install php5 sudo apt-get install mysql-server sud ...
- RabbitMQ消息队列:ACK机制
每个Consumer可能需要一段时间才能处理完收到的数据.如果在这个过程中,Consumer出错了,异常退出了,而数据还没有处理完成,那么 非常不幸,这段数据就丢失了. 因为我们采用no-ack的方式 ...
- 70 数组的Kmin算法和二叉搜索树的Kmin算法对比
[本文链接] http://www.cnblogs.com/hellogiser/p/kmin-of-array-vs-kmin-of-bst.html [分析] 数组的Kmin算法和二叉搜索树的Km ...
- 集群ssh服务和免密码登录的配置
安装Hadoop之前,由于集群中大量主机进行分布式计算需要相互进行数据通信,服务器之间的连接需要通过ssh来进行,所以要安装ssh服务,默认情况下通过ssh登录服务器需要输入用户名和密码进行连接,如果 ...
- Java for LeetCode 140 Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- ubuntu命令行相关命令使用心得
一.Ubuntu解压缩zip,tar,tar.gz,tar.bz2 ZIP zip可能是目前使用得最多的文档压缩格式.它最大的优点就是在不同的操作系统平台,比如Linux, Windows以及Mac ...
- 在Win8中创建热点,共享网络
在Win8中创建热点,共享网络 办公室中,我独享10M光纤,没什么要下的,便想利用来更新下Ipad里面的程序,下点公开课.那在不利用软件[用很多wifi共享的软件],从win7开始 系统本身就自带相关 ...
- CentOS 6.5 下安装 Kibana5
1. 导入Elastic PGP Key 执行命令 rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch 2. 安装Kiban ...
- Xamarin.Android开发实践(十五)
Xamarin.Android学习之应用程序首选项 一.前言 任何App都会存在设置界面,如果开发者利用普通控件并绑定监听事件保存设置,这 一过程会非常的枯燥,而且耗时.我们可以看到Android系统 ...