- Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathbinary_search.cpp
63 lines (55 loc) · 1.36 KB
/
binary_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*Author : Abdallah Hemdan */
/***********************************************/
/* Dear online judge:
* I've read the problem, and tried to solve it.
* Even if you don't accept my solution, you should respect my effort.
* I hope my code compiles and gets accepted.
* ___ __
* |\ \|\ \
* \ \ \_\ \
* \ \ ___ \emdan
* \ \ \\ \ \
* \ \__\\ \_\
* \|__| \|__|
*/
#include<iostream>
#defineall(v) v.begin(),v.end()
#definemp make_pair
#definepb push_back
#defineendl'\n'
typedeflonglongint ll;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
usingnamespacestd;
intBinarySearch(vector<int> Arr, int n, int Target) {
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (Arr[mid] == Target)
return mid;
elseif (Arr[mid] < Target) {
l = mid + 1;
}
elseif (Arr[mid] > Target) {
r = mid - 1;
}
}
return -1;
}
voidFindinFirst(vector<int> First, int n, vector<int> Second, int k) {
for (int i = 0; i < k; i++)
cout << BinarySearch(First,n, Second[i]) << "";
}
intmain() {
int n;
cin >> n;
vector<int> First(n);
for (size_t i = 0; i < n; i++)
cin >> First[i];
int k;
cin >> k;
vector<int> Second(k);
for (size_t i = 0; i < k; i++)
cin >> Second[i];
FindinFirst(First, n, Second, k);
}