http://acm.hdu.edu.cn/showproblem.php?pid=3763

题意:

两组数据

看重复的有多少

如果每输入一个就去查找的话O(n^2) 会超时

所以可以用二法

第一组数据输入

然后第二组数据输入 在第一组数据用二分查 这样O(n*logn)

不能用set等容器 虽然时间复杂度是一样的但是 MLE

还有一种O(n)的做法

枚举a数组中的元素,然后用一个指针指向b数组中第一个大于等于a数组当前元素的数,不断移动这个指针即可

 #include <iostream>
#include <stdio.h> using namespace std; int a[];
int b[];
int n;
bool search(int x)
{
int l = , r = n-;
int mid = (l+r)/;
while(l <= r)
{
if (a[mid] < x){
l = mid+;
mid = (l+r) / ;
}
else if (a[mid] > x)
{
r = mid-;
mid = (l+r)/;
}
else return true;
}
return false; }
int main()
{
freopen("in.txt", "r", stdin);
int m, ans = ;
while(~scanf("%d%d", &n, &m))
{
ans = ;
if ( m== && n == ) break;
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
int tmp;
for (int i = ; i < m; i++)
{
scanf("%d", &tmp);
if (search(tmp))
ans++;
}
printf("%d\n", ans);
}
return ;
}

HDU 3763 CDs的更多相关文章

  1. HDU 3763 CD【二分查找】

    解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每 ...

  2. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. hdu1513 Palindrome

    思路: dp+滚动数组. 实现: #include <iostream> #include <cstdio> #include <string> #include ...

  2. 安装mysql时 make 时 提示 redeclaration of C++ built-in type ‘bool’ 错误

    安装mysql时 make 时 提示 redeclaration of C++ built-in type ‘bool’ 错误 由于gcc-c++是在./configure后进行编译的 重新./con ...

  3. webapi之fiddler头设置

    Host: localhost:16648Connection: keep-aliveContent-Length: 36Accept: application/json, text/javascri ...

  4. react基础语法(二)常用语法如:样式 ,自定义属性,常用表达式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 原生js的容易忽略的相似点(一)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. HDU 5410 CRB and His Birthday (01背包,完全背包,混合)

    题意:有n种商品,每种商品中有a个糖果,如果买这种商品就送多b个糖果,只有第一次买的时候才送.现在有m元,最多能买多少糖果? 思路:第一次买一种商品时有送糖果,对这一次进行一次01背包,也就是只能买一 ...

  7. HDU 5380 Travel with candy (贪心,单调队列)

    题意: 有n+1个城市按顺序分布在同一直线上,现在需从0号城市按顺序走到n号城市(保证可达),从0号城市到i号城市需要消耗ai个糖果,每个城市都可以通过买/卖糖果来赚取更多的钱,价格分别是buyi和s ...

  8. nodejs 安装 淘宝镜像

    临时使用 npm --registry https://registry.npm.taobao.org install express 2.持久使用 npm config set registry h ...

  9. java程序中中文没有乱码,存入数据库后中文乱码问题

    jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/sys_user?useOldAliasMetadataBe ...

  10. 数据结构——RMQ

    RMQ 今天临放学前终于是学会了RMQ,特此写一篇题解来缅怀 RMQ是一种数据结构,用途是查询区间内最大值或最小值 或者你所要求的任意条件,主要思想是二进制的思想,其中还用到了dp的思想, 是一种非常 ...