-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10816.cpp
More file actions
71 lines (67 loc) · 1.01 KB
/
10816.cpp
File metadata and controls
71 lines (67 loc) · 1.01 KB
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
64
65
66
67
68
69
70
71
// 10816. 숫자 카드 2
// 2019.07.18
// 이분 탐색
#include<iostream>
#include<algorithm>
using namespace std;
int arr[500005];
int n;
int lowerBound(int target)
{
int start = 0;
int end = n;
// start = end로 가능한 후보가 1개로 확정될 경우 while문을 탈출
while (start < end)
{
int mid = (start + end) / 2;
if (arr[mid] >= target)
{
end = mid;
}
else
{
start = mid + 1;
}
}
return start;
}
int upperBound(int target)
{
int start = 0;
int end = n;
// start = end로 가능한 후보가 1개로 확정될 경우 while문을 탈출
while (start < end)
{
int mid = (start + end) / 2;
if (arr[mid] > target)
{
end = mid;
}
else
{
start = mid + 1;
}
}
return start;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr, arr + n);
int m;
cin >> m;
for (int i = 0; i < m; i++)
{
int t;
cin >> t;
cout << upperBound(t) - lowerBound(t) << " ";
}
return 0;
}