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. asp.net MVC中实现调取web api

    public ActionResult Index(string city) { if (string.IsNullOrEmpty(city)) { city = "上海"; } ...

  2. gp服务输出的结果文件输出到绝对路径

    gp服务跟本地用arcmap执行gp有个不同,就是输出的文件一般只能输出到arcgis server默认的output目录里面(arcgis server有此限制,无论怎么配还是写到output目录里 ...

  3. Redis学习笔记(四)集合进阶

    1.组合与关联多个集合 差集: SDIFF key1 [key2...](返回存在于key1但不存在其他集合中的元素) SDIFFSTORE destination key1 [key2...](将存 ...

  4. 第16周翻译:SQL Server中的事务日志管理,级别3:事务日志、备份和恢复

    源自: http://www.sqlservercentral.com/articles/Stairway+Series/73779/ 作者: Tony Davis, 2011/09/07 翻译:刘琼 ...

  5. webpack3整理(第三节/满三节)------(base.config文件解释)

    'use strict' const path = require('path') const utils = require('./utils') const config = require('. ...

  6. 在Apache服务器中禁用option

    在apache禁止 http OPTIONS方法. apache disable http OPTIONS method 2013-04-17 09:27 4050人阅读 评论(1) 收藏 举报  分 ...

  7. 洛谷 P1216 [USACO1.5]数字三角形 Number Triangles(水题日常)

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...

  8. Django-常用设置(settings.py)

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 当前项目的根目录,Django会依此来定位工程内的相关文件 ...

  9. 原生JS--各种兼容性写法总结

    <script> var oEvent = evt || event; ========================================================== ...

  10. Postman 安装及使用入门教程 | 前后台 写接口的 徐工给的

    https://www.cnblogs.com/mafly/p/postman.html