解题思路:给出两个数列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. VS2008中C++打开Excel(MFC)

    VS2008中C++打开Excel(MFC)——摘自网络,并加以细化 第一步:建立project(新建项目) 英文版 中文版 选择C++下的MFC Application(基于对话框的项目) 英文版 ...

  2. xshell 连接 ubuntu 16.04报错

    outgoing encryption 错误   使用xshell和xftp连接 ubuntu 16.04 时出现找不到匹配的 outgoing encryption 算法的错误提示. 问题阐述: 在 ...

  3. RESTful API设计方法

    1.如果已经开始逐步的接触到了RESTful API设计方法的朋友,首先要对HTTP/HTTPS有一个大致的了解,虽然本身和RESTful API没有什么关系.但是对于增加网站的安全性还是十分重要的, ...

  4. iview关于menu结合router问题

    #iview关于menu结合router问题 1. Menu.Item下router问题: 直接在Menu标签上绑定on-select事件,可以获取到name(name为元素绑定name) <M ...

  5. 【CodeForces 987C】Three displays

    [链接] 我是链接,点我呀:) [题意] [题解] 动态规划 设dp[i][j]表示前i个数字,选了j个的最小花费. dp[i][j] = min(dp[k][j-1]+b[i]);//其中a[i]& ...

  6. (5)全局异常捕捉【从零开始学Spring Boot】

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...

  7. String 基本使用方法, 以及要注意的事项

    package chengbaoDemo; public class Test01 { public static void main(String[] args) { //字符串的两种创建形式 St ...

  8. HDU5979 Convex

    /* HDU5979 Convex http://acm.hdu.edu.cn/showproblem.php?pid=5979 计算几何 三角形面积公式 * * */ #include <cs ...

  9. 工具-VS CODE安装

    在Linux下的安装 1.下载tar.gz文件包, 2.要注意加一条命令,这样在任何目录下就可以使用code .直接启动应用程序了 1 sudo ln -s /path/to/vscode/Code ...

  10. BA--暖通系统常见设计细节要点

    (一)系统设计问题 1.水泵在系统的设计位置: 一般而言,冷冻水泵应设在冷水机组前端,从末端回来的冷冻水经过冷冻水泵打回冷水机组:冷却水泵设在冷却水进机组的水路上,从冷却塔出来的冷却水经冷却水泵打回机 ...