hdu 3763 CD(二分)
2017-11-19 13:39
288 查看
CD
TimeLimit: 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; }
相关文章推荐
- HDU 3763 CD【二分查找】
- hdu 3763 CD(二分查找)
- HDU 3763 CD(二分查找)
- hdu 3763 CD(二分查找)
- hdu 3763 CD(二分)
- HDU 3763 CD
- HDU 3763 C D(二分)
- hdu 3763 二分
- HDU 3763-CD
- hdoj 3763 CD(二分查找)
- hdu 3763 CD
- HDOJ/HDU 2141 Can you find it? 二分搜索优化
- HDU 2141 Can you find it?(数组找数字,二分)
- 二分 HDU Problem G
- Hdu 3622 Bomb Game 2-SAT+二分
- hdu------(4302)Holedox Eating(树状数组+二分)
- hdu5432 Pyramid Split(二分)
- HDU 2199 Can you solve this equation? 二分
- HDU 5875 二分+st表
- HDU ACM 2063 过山车(二分匹配)