-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01920_BinarySearch.cpp
More file actions
52 lines (47 loc) · 878 Bytes
/
01920_BinarySearch.cpp
File metadata and controls
52 lines (47 loc) · 878 Bytes
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
#include <iostream>
#include <algorithm>
#define MAX 100000
using namespace std;
int n, m;
int a[MAX], b[MAX];
void input()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> m;
for (int i = 0; i < m; i++)
cin >> b[i];
}
int main()
{
input();
sort(a, a + n);
for (int i = 0; i < m; i++)
{
int start = 0;
int end = n - 1;
while (start <= end)
{
int mid = (start + end) / 2;
if (b[i] == a[mid])
{
cout << 1 << '\n';
break;
}
else if (b[i] < a[mid])
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
if (start > end)
{
cout << 0 << '\n';
}
}
return 0;
}