Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions jiyunpar/binary_search/10816.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;


int n, m;
vector<int> seq;
vector<int> 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);
}
38 changes: 38 additions & 0 deletions jiyunpar/binary_search/10816_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;


int n, m;
vector<int> seq;
vector<int> 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);
}