二分搜索 AOJ ALDS1_4_B:Binary Search

题目网址链接:

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_B

Search - Binary Search

Time Limit : 1 sec, Memory Limit : 131072 KB 

Search II

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output

Print C in a line.

Constraints

  • Elements in S is sorted in ascending order
  • n ≤ 100000
  • q ≤ 50000
  • 0 ≤ an element in S ≤ 109
  • 0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
1 2 3
1
5

Sample Output 2

0

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

题意:

请编写一个程序,输入包含n个整数的数列S以及包含q个不重复整数的数列T,输出既包含于T也包含于S的整数的个数C

题目分析:

二分查找。

方法一:

利用C++ STL 中自带的二分查找函数(需要借助容器vector)

binary_search(v.begin(), v.end(), number_to_find);

C++代码实现如下:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    int n, q, x;
    int ans = 0;
    vector<int> a, b;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        a.push_back(x);
    }
    cin >> q;
    for (int j = 0; j < q; j++)
    {
        cin >> x;
        b.push_back(x);
    }
    for (vector<int>::iterator it = b.begin(); it != b.end(); it++)
    {
        if (binary_search(a.begin(), a.end(), *it))
            ans++;
    }
    cout << ans << endl;
    return 0;
}


方法二:

自编二分查找函数

遍历数列T的每个元素,对于每个元素,利用find二分查找这个元素是否在数列S中。

如果在,则计数器ans++

对于二分查找函数

bool find(int *a, int n, int m, int left, int right, int sum) ,

说明如下:

a代表数组,n是元素个数,m是要查询的数,left是二分最左端,right是二分最右端,sum记录递归的次数

C++代码实现如下:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int a[100001];
int b[50001];

bool find(int *a, int n, int m, int left, int right, int sum) // a代表数组,n是元素个数,m是要查询的数,left是二分最左端,right是二分最右端,sum记录递归的次数
{
	if (pow(2, sum)  > n + 10) // 如果递归次数超过二分正常的次数log2(n)
		return false;
	if (a[left] == m)
		return true;
	if (a[right] == m)
		return true;
	int mid = (left + right) / 2;
	if (m == a[mid])
		return true;
	else if (m < a[mid])
	{
		right = mid;
		find(a, n, m, left, right, sum + 1); // 递归
	}
	else
	{
		left = mid;
		find(a, n, m, left, right, sum + 1); // 递归
	}
}

int main()
{
	int n, q;
	int sum = 0;
	int ans = 0;
	scanf ("%d", &n);
	for (int i = 0; i < n; i++)
		scanf ("%d", &a[i]);
	scanf ("%d", &q);
	for (int i = 0; i < q; i++)
		scanf ("%d", &b[i]);
	sort(a, a + n);
	for (int i = 0; i < q; i++)
		if (find(a, n, b[i], 0, n - 1, sum)) // 二分查找
			ans++;
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41705423/article/details/81781919
今日推荐