hdu 3763 CD(二分)

来源:互联网 发布:易推微博助手软件 编辑:程序博客网 时间:2024/05/16 13:07

CD

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


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 31231240 0
 

Sample Output
2
 

#include<stdio.h>#include<istream>#include<algorithm>using namespace std;int s[10000005];int fun(int x,int n){int mid,low,high;low=1;high=n;mid=(low+high)/2;while(low<=high){mid=(low+high)/2;if(x<s[mid])high=mid-1;else if(x>s[mid])low=mid+1;else return 1;}return 0;}int main(){int m,n,i,j,a;int sum;while(scanf("%d%d",&n,&m),n+m){sum=0;for(i=1;i<=n;i++){scanf("%d",&s[i]);}for(j=1;j<=m;j++){scanf("%d",&a);sum+=fun(a,n);}printf("%d\n",sum);}return 0;} 


0 0