백준 10816 숫자 카드 2

supway·2022년 2월 26일
0

백준

목록 보기
42/62

백준 10816

uppper_bound랑 lower_bound를 구현하는 문제

#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[500001];
int lowerBound(int t) {
	int l = 0; 
	int r = n;
	
	while (l < r) {
		int m = (l + r) / 2;
		
		if (arr[m] >= t) {
			r = m;
		}
		else {
			l = m + 1;
		}
	}
	return r;
}
int upperBound(int t) {
	int l = 0;
	int r = n;

	while (l < r) {
		int m = (l + r) / 2;

		if (arr[m] > t) {
			r = m;
		}
		else {
			l = m + 1;
		}
	}
	return r;
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; i++) cin >> arr[i];

	cin >> m;

	vector<int> v;
	while (m--) {
		int x;
		cin >> x;
		v.push_back(x);
	}

	sort(arr, arr + n);

	for (auto c : v) {
		//cout << upperBound(c) - lowerBound(c) << " ";
		cout << upper_bound(arr, arr + n, c) - lower_bound(arr, arr + n, c) << " ";
	}
}
profile
개발잘하고싶은사람

0개의 댓글