1196. History Exam

1196. History Exam

Time Limit: 2.0 second
Memory Limit: 16 MB
Professor of history decided to simplify the examination process. At the exam, every student should write a list of historic dates she knows (she should write the years only and, of course, must be able to explain what event took place in this or that year). Professor has a list of dates that students must know. In order to decide upon the student's mark, Professor counts the number of dates in the student's list that are also present in his list. The student gets her mark according to the number of coincidences.
Your task is to automatize this process. Write a program that would count the number of dates in the student's list that also occur in Professor's list.

Input

The first line contains the number  N of dates in Professor's list, 1 ≤  N ≤ 15000. The following N lines contain this list, one number per line. Each date is a positive integer not exceeding 10 9. Professor's list is sorted in non-descending order. The following line contains the number  Mof dates in the student's list, 1 ≤  M ≤ 10 6. Then there is the list itself; it is unsorted. The dates here satisfy the same restriction. Both in Professor's and in the student's lists dates can appear more than once.

Output

Output the number of dates in the student's that are also contained in Professor's list.

Sample

input output
2
1054
1492
4
1492
65536
1492
100
2


我不知道为什么之前不用二分的时候,明明结果正确,也报错。。(Wrong answer,,无关时间问题)


#include <iostream>
using namespace std;

int a[15000], b[1000000];

int main()
{
	int n, m, i, j, num = 0, l, r, k;
	cin>>n;
	for (i=0; i<n; i++)
		cin>>a[i];
	cin>>m;
	for (i=0; i<m; i++)
		cin>>b[i];
	for (i=0; i<m; i++)
	{
		l = 0;
		r = n - 1;
		while (l <= r)
		{
			k = (l + r) / 2;
			if (b[i] == a[k])
			{
				num++;
				break;
			}
			else if (b[i] < a[k])
				r = k - 1;
			else
				l = k + 1;
		}
	}
	cout<<num<<endl;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值