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

CD

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 627    Accepted Submission(s): 280

Problem Description
Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. They have decided to sell one of each of the CD titles that they both own. How many CDs can Jack and Jill sell?
Neither Jack nor Jill owns more than one copy of each CD.
 
Input
The input consists of a sequence of test cases. The first line of each test case contains two non-negative integers N and M, each at most one million, specifying the number of CDs owned by Jack and by Jill, respectively. This line is followed by N lines listing the catalog numbers of the CDs owned by Jack in increasing order, and M more lines listing the catalog numbers of the CDs owned by Jill in increasing order. Each catalog number is a positive integer no greater than one billion. The input is terminated by a line containing two zeros. This last line is not a test case and should not be processed.
 
Output
For each test case, output a line containing one integer, the number of CDs that Jack and Jill both own.
 
Sample Input
3 3
1
2
3
1
2
4
0 0
 
Sample Output
2
 
#include<stdio.h>
#include<string.h>
int a[1000005],b[1000005];
int bsearch(int a[],int n,int t)
{
int x,y,m;
x=1;
y=n;
while(x<=y)
{
m=(x+y)/2;
if(a[m]==t)
return 1;
else
{
if(a[m]>t)
y=m-1;
else
x=m+1;
} }
return 0;
}
int main()
{
int n,m,i,t,num;
while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
{
num=0;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=m;i++)
{
scanf("%d",&b[i]);
num+=bsearch(a,n,b[i]);
}
printf("%d\n",num);
}
}

  

HDU 3763 CD【二分查找】的更多相关文章

  1. Can you find it? HDU - 2141 (二分查找)

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

  2. HDU 1969 Pie(二分查找)

    Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...

  3. (step4.1.2)hdu 1969(Pie——二分查找)

    题目大意:n块馅饼分给m+1个人,每个人的馅饼必须是整块的,不能拼接,求最大的. 解题思路: 1)用总饼的体积除以总人数,得到每个人最大可以得到的V.但是每个人手中不能有两片或多片拼成的一块饼. 代码 ...

  4. HDU 2578(二分查找)

    686MS #include <iostream> #include <cstdlib> #include <cstdio> #include <algori ...

  5. HDU 5878---预处理+二分查找

    给一个数n,让你求一个大于等于n的最小的满足题意中2^a*3^b*5^c*7^d的数字. 思路: #include<iostream> #include<cstdio> #in ...

  6. hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  7. hdu 2141 Can you find it?(二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. /* x^2+6*x- ...

  8. HDU 4614 线段树+二分查找

    Vases and Flowers 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4614 Problem Description Alice is s ...

  9. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

随机推荐

  1. RabbitMQ学习笔记(5)----RabbitMQ整合Spring

    在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit. Sprin ...

  2. gulp给文件后添加md5时间戳

    这里为总的方法,实际项目中拷贝出来的,底下有详细的总结以及只针对添加时间戳的方法 1 // 引入 gulp及组件 var gulp = require('gulp'), autoprefixer = ...

  3. ajax发送请求是图标转圈圈实现

    css部分 .load-img{ //控制图标大小width:40px;height:40px;margin:100px;border-radius:50%;-webkit-animation:cir ...

  4. Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案

    点关注,不迷路:持续更新Java架构相关技术及资讯热文!!! 相信使用Mybaits的小伙伴们一定会经常编写类似如下的代码: 可以看到 userMapper 下有个红色警告.虽然代码本身并没有问题,能 ...

  5. nginx安装http2.0协议

    1.HTTP2协议 HTTP 2.0 的主要目标是改进传输性能,实现低延迟和高吞吐量.从另一方面看,HTTP 的高层协议语义并不会因为这次版本升级而受影响.所有HTTP 首部.值,以及它们的使用场景都 ...

  6. Eclipse maven工程 Missing artifact com.sun:tools:jar:1.7.0:system 解决方法

    解决方案一:通过maven取运行时参数,eclipse提供的环境变量,基本类似System.getProperty("java.home") <dependency> ...

  7. sklearn学习汇总

    该博主总结的很好,https://www.cnblogs.com/hellcat/p/7531789.html 1.kaggle给出的导图 2.转化成树图: 3.sklearn工具导图

  8. C++学习笔记(转)

    http://www.cnblogs.com/maowang1991/p/3290321.html 以下内容为自己一年多的C++学习心得,纯原创,转载请注明源地址. 一年多的C++学习过程中,自己阅读 ...

  9. HDU 2857 Mirror and Light

    /* hdu 2857 Mirror and Light 计算几何 镜面反射 */ #include<stdio.h> #include<string.h> #include& ...

  10. L - 还是畅通工程

    L - 还是畅通工程   思路:水! #include<cstdio> #include<cstring> #include<iostream> #include& ...