알고리즘
BOJ 10816 : 숫자 카드 2
minbear
2023. 6. 15. 18:05
#BOJ 10816 : 숫자 카드 2
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,0
www.acmicpc.net
#코드
#include<iostream>
#include<algorithm>
using namespace std;
int n, m;
int arr_n[500002];
int arr_m[500002];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr_n[i];
}
sort(arr_n, arr_n + n);
cin >> m;
while (m--) {
int num = 0;
cin >> num;
int start = 0;
int End = n;
int end_idx = 0;
int st_idx = 0;
while (start < End) {
int idx = (start + End) / 2;
if (arr_n[idx] > num)
{
End = idx;
}
else {
start = idx + 1;
}
}
end_idx = start;
start = 0;
End = n;
while (start < End) {
int idx = (start + End) / 2;
if (arr_n[idx] >= num)
{
End = idx;
}
else {
start = idx + 1;
}
}
st_idx = start;
cout << end_idx - st_idx << '\n';
}
return 0;
}