From b3f599e9b68e2d6cfbb64d6de532f163caa50d65 Mon Sep 17 00:00:00 2001 From: flowjiyun Date: Tue, 21 Feb 2023 13:51:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20solved=20=EC=88=AB=EC=9E=90=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=202=20/=20binary=20serach=20with=20lower&upper=20boun?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jiyunpar/binary_search/10816.cpp | 64 ++++++++++++++++++++++++++++++ jiyunpar/binary_search/10816_1.cpp | 38 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 jiyunpar/binary_search/10816.cpp create mode 100644 jiyunpar/binary_search/10816_1.cpp diff --git a/jiyunpar/binary_search/10816.cpp b/jiyunpar/binary_search/10816.cpp new file mode 100644 index 0000000..461c1c3 --- /dev/null +++ b/jiyunpar/binary_search/10816.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +using namespace std; + + +int n, m; +vector seq; +vector target; + +int upperbound(int val) +{ + int st = 0; + int end = n; + while (st < end) + { + int mid = (st + end) / 2; + if (seq[mid] > val) + end = mid; + else + st = mid + 1; + } + return (st); +} + +int lowerbound(int val) +{ + int st = 0; + int end = n; + while (st < end) + { + int mid = (st + end) / 2; + if (seq[mid] >= val) + end = mid; + else + st = mid + 1; + } + return (st); +} + +int main(void) +{ + ios::sync_with_stdio(0); + cin.tie(0); + seq.reserve(500000); + cin >> n; + for (int i = 0; i < n; ++i) + { + int val; + cin >> val; + seq.push_back(val); + } + sort(seq.begin(), seq.end()); + cin >> m; + while (m--) + { + int val; + cin >> val; + cout << upperbound(val) - lowerbound(val) << " "; + } + return (0); +} \ No newline at end of file diff --git a/jiyunpar/binary_search/10816_1.cpp b/jiyunpar/binary_search/10816_1.cpp new file mode 100644 index 0000000..76157a0 --- /dev/null +++ b/jiyunpar/binary_search/10816_1.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +using namespace std; + + +int n, m; +vector seq; +vector target; + +// using STL lowerbound upperbound +// return val : if array -> pointer +// if stl container -> iterator + +int main(void) +{ + ios::sync_with_stdio(0); + cin.tie(0); + seq.reserve(500000); + cin >> n; + for (int i = 0; i < n; ++i) + { + int val; + cin >> val; + seq.push_back(val); + } + sort(seq.begin(), seq.end()); + cin >> m; + while (m--) + { + int val; + cin >> val; + cout << upper_bound(seq.begin(), seq.end(), val) - lower_bound(seq.begin(), seq.end(), val) << " "; + } + return (0); +} \ No newline at end of file