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

题意:

两组数据

看重复的有多少

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

所以可以用二法

第一组数据输入

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

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

还有一种O(n)的做法

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

  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int a[];
  7. int b[];
  8. int n;
  9. bool search(int x)
  10. {
  11. int l = , r = n-;
  12. int mid = (l+r)/;
  13. while(l <= r)
  14. {
  15. if (a[mid] < x){
  16. l = mid+;
  17. mid = (l+r) / ;
  18. }
  19. else if (a[mid] > x)
  20. {
  21. r = mid-;
  22. mid = (l+r)/;
  23. }
  24. else return true;
  25. }
  26. return false;
  27.  
  28. }
  29. int main()
  30. {
  31. freopen("in.txt", "r", stdin);
  32. int m, ans = ;
  33. while(~scanf("%d%d", &n, &m))
  34. {
  35. ans = ;
  36. if ( m== && n == ) break;
  37. for (int i = ; i < n; i++)
  38. scanf("%d", &a[i]);
  39. int tmp;
  40. for (int i = ; i < m; i++)
  41. {
  42. scanf("%d", &tmp);
  43. if (search(tmp))
  44. ans++;
  45. }
  46. printf("%d\n", ans);
  47. }
  48. return ;
  49. }

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. Elasticsearch (2) - 映射

    常用映射类型 核心的字段类型如下: String 字符串包括text和keyword两种类型: 1.text analyzer 通过analyzer属性指定分词器. 下边指定name的字段类型为tex ...

  2. js函数中获得当前被点击元素

    问题描述:在html页面中点击<a>或者’按钮‘,进入js中的函数,在js函数中获得被点击那个<a>或‘按钮’元素 解决方法:方法一: html中: <a>标签:& ...

  3. 使用JavaScript给对象修改注册监听器

    我们在开发一些大型前端项目时,会遇到这样一种情况,某个变量上有个字段.我们想知道是哪一段程序修改了这个变量上的字段.比如全局变量window上我们自定义了一个新字段_name,我们想知道到底有哪些程序 ...

  4. github 下载全部项目

    从github下载资料过程中,有些项目含有子模块,有时通过git clone 或者下载zip方式项目可能会缺少文件,因此需要执行 git submodule update --init --recur ...

  5. 使用python书写的小说爬虫

    1.写了一个简单的网络爬虫 初期1 (后期将会继续完善) #小说的爬取 import requests import random from bs4 import BeautifulSoup base ...

  6. 7-Java-C(四平方和)

    题目描述: 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和. 如果把0包括进去,就正好可以表示为4个数的平方和. 比如: 5 = 0^2 + 0^2 + 1^2 + ...

  7. 7-Java-C(搭积木)

    题目描述: 小明最近喜欢搭数字积木, 一共有10块积木,每个积木上有一个数字,0~9. 搭积木规则: 每个积木放到其它两个积木的上面,并且一定比下面的两个积木数字小. 最后搭成4层的金字塔形,必须用完 ...

  8. Spring-02 Java配置实现IOC

    Java配置 Spring4推荐使用java配置实现IOC Spring boot也推荐采用java配置实现IOC 在实际项目中,一般采用注解配置业务bean,全局配置使用Java配置. Java配置 ...

  9. 【C语言项目】贪吃蛇游戏(上)

    目录 00. 目录 01. 开发背景 02. 功能介绍 03. 欢迎界面设计 3.1 常用终端控制函数 3.2 设置文本颜色函数 3.3 设置光标位置函数 3.4 绘制字符画(蛇) 3.5 欢迎界面函 ...

  10. git 超时 时长 设置?

    [Pipeline] { (Checkout) [Pipeline] checkout > git.exe rev-parse --is-inside-work-tree # timeout=1 ...