前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Binary Search

Binary Search

作者头像
废江_小江
发布于 2022-09-05 06:32:15
发布于 2022-09-05 06:32:15
31500
代码可运行
举报
文章被收录于专栏:总栏目总栏目
运行总次数:0
代码可运行

Question

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

n ≤ 10000

q ≤ 500

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

3 1 2

1

5

Sample Output 2

0

Sample Input 3

5

1 1 2 2 3

2

1 2

Sample Output 3

2

Meaning

使用二分搜索,效率就会很大提高。但是需要注意,二分搜索的前提是数组已经排序好了。那么排序一般最高的效率希尔排序也是0(n1.25),岂不是效率还降低了?相对于O(n),这里题目限制说了

给出的数据就是按升序排列的,那么时间复杂度就是0(qlogn)

Coding

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<iostream>
#include<cstdio>
using namespace std;
int A[100005];
int BinarySearch(int a[], int size, int key) {
	int l = 0;
	int r = size - 1;
	while (l <= r) {
		int mid = l + (r - l) / 2;
		if (key == a[mid])
			return true;
		else if (key > a[mid])
			l = mid + 1;
		else
			r = mid - 1;
	}
	return false;//没有找到
}
int main() {
	int n, q, key, sum = 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", &key);
		if (BinarySearch(A,n,key))
			sum++;
	}
	printf("%d\n", sum);
}

Summary

二分搜索使用前需要对数组进行排序;

标准二分搜索算法:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
int BinarySearch(int a[], int size ,int p){
	int L=0; //查找区间的左端点
	int R=size -1;//查找区间的的右端点
	while(L<=R){
		int mid =L+(R-L)/2;
		if(p == a[mid])
			return mid;
		else if(p>a[mid])
			L=mid +1;
		else
			R=mid-1; 
	}
	return -1;
}

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权

转载请注明原文链接:Binary Search

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-09),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Linear Search
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.
废江_小江
2022/09/05
2700
HDU 3333 Turing Tree (线段树)
Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4768    Accepted Submission(s): 1686 Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems ab
ShenduCC
2018/04/27
5370
Merge Sort
Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.
废江_小江
2022/09/05
5710
Exhaustive Search
Write a program which reads a sequence A of n elements and an integer M, and outputs “yes” if you can make M by adding elements in A, otherwise “no”. You can use an element only once.
废江_小江
2022/09/05
3210
Subsequence (POJ - 3061)(尺取思想)
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.
Lokinli
2023/03/09
1830
【二分搜索】最大化最小值:POJ2456
题目:POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36441 Accepted: 16640 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x
灯珑LoGin
2022/10/31
2760
海战(线段树)- HDU 4027
这一篇是典型的线段树算法,这个算法在日常工作中可能非常少见,因为可以被常规算法所取代,但是在问题达到一定数量级之后,常规算法是很难搞定类似问题的,可以说线段树是高级算法中非常低调的一种,也许在某些关键时刻能让你化险为夷。
ACM算法日常
2018/08/07
5700
海战(线段树)- HDU 4027
HDU 2665 Kth number(可持续化线段树)
Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9213    Accepted Submission(s): 2868 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The f
ShenduCC
2018/04/27
4480
04-树6 Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
废江_小江
2022/09/05
2630
POJ 2104 K-th Number(主席树)
Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segm
attack
2018/04/12
6330
HDU 3450 Counting Sequences(线段树)
Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 2335    Accepted Submission(s): 820 Problem Description For a set of sequences of integers{a1,a2,a3,...an}, we define a sequence
ShenduCC
2018/04/27
5210
HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2489    Accepted Submission(s): 759 Problem Description When the magic ball game turns up, Kimi immediately falls in it. The inter
ShenduCC
2018/04/27
6660
(四)算法基础——二分算法
求下面方程的一个根:f(x) = x3 -5x2+10x-80 = 0 若求出的根是a,则要求 |f(a)| <= 10^-6
小点点
2022/12/12
5180
(四)算法基础——二分算法
Max Sum(优化)- HDU 1003
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
ACM算法日常
2018/08/07
3720
Max Sum(优化)- HDU 1003
HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again
Calling Extraterrestrial Intelligence Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4083    Accepted Submission(s): 2140 Problem Description A message from humans to extraterrestrial intelli
Gxjun
2018/03/21
8010
实现一个二分搜索算法,搜索指定元素在已排序数组中的位置。(递归或者非递归实现)
简介:实现一个二分搜索算法,搜索指定元素在已排序数组中的位置。(递归或者非递归实现)
GeekLiHua
2025/01/21
740
POJ 2374 Fence Obstacle Course(线段树+动态规划)
Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2524 Accepted: 910 Description Farmer John has constructed an obstacle course for the cows' enjoyment. The course consists of a sequence of N fences (1 <= N <=
ShenduCC
2018/04/27
6720
Code Forces 652D Nested Segments(离散化+树状数组)
 Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the numb
ShenduCC
2018/04/26
6290
AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)
A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or pr
Angel_Kitty
2018/04/09
6760
AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)
Doubly Linked List
Your task is to implement a double linked list.
废江_小江
2022/09/05
2070
相关推荐
Linear Search
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档