您的位置:首页 > 其它

hdu 3763 CD(二分)

2017-11-19 13:39 288 查看


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 3
1
2
3
1
2
4
0 0

 

Sample Output

2

 

我用数组下标 标记,,莫名的过了  题目数据较水  数组的大小  开到一百万(bool)即可 ,不用开到十亿。。 

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
bool b[10000010];
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)){
if(n==0&&m==0) break;
int a,ans=0;
// map<int ,bool>Q;
memset(b,false,sizeof(b));
for(int i=0;i<n;i++){
scanf("%d",&a);
b[a]=true;
}
for(int i=0;i<m;i++){
scanf("%d",&a);
if(b[a]==true) ans++;
}
printf("%d\n",ans);
}
return 0;
}


二分代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: