Algorithm π§π»π»/λ°±μ€(BOJ)
[λ°±μ€,c++] 10815λ² - μ«μμΉ΄λ
dkswnkk
2021. 10. 24. 22:07
10815λ²: μ«μ μΉ΄λ
첫째 μ€μ μκ·Όμ΄κ° κ°μ§κ³ μλ μ«μ μΉ΄λμ κ°μ N(1 ≤ N ≤ 500,000)μ΄ μ£Όμ΄μ§λ€. λμ§Έ μ€μλ μ«μ μΉ΄λμ μ νμλ μ μκ° μ£Όμ΄μ§λ€. μ«μ μΉ΄λμ μ νμλ μλ -10,000,000λ³΄λ€ ν¬κ±°λ κ°κ³ , 10,
www.acmicpc.net
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N;
vector<int>v;
for (int i = 0; i < N; i++) {
int number; cin >> number;
v.push_back(number);
}
sort(v.begin(), v.end());
cin >> M;
for (int i = 0; i < M; i++) {
int start = 0;
int end = N - 1;
int check;
cin >> check;
while (start <= end) {
int mid = (start + end) / 2;
if (v[mid] == check) { //μ°Ύλκ°μ΄ μμΌλ©΄ μΆλ ₯
cout << 1 << " ";
break;
}
else if (v[mid] < check) { //μ°Ύλκ°μ΄ λ ν¬λ©΄ μ€λ₯Έμͺ½νμ
start = mid + 1;
}
else { //μ°Ύλ κ°μ΄ μμΌλ©΄ μΌμͺ½ νμ
end = mid - 1;
}
if (start > end) cout << 0 << " ";
}
}
}