https://www.acmicpc.net/problem/2776
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
/******** 전역변수 ********/
/******** 함 수 ********/
int main(void) {
/******** C++ INIT ********/
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
/******** 지역변수 ********/
int input1;
int input2;
int input3;
int num;
int left, right, mid;
vector<int> note1;
vector<int> note2;
/******** 구 현 ********/
cin >> input1;
for (int i = 0; i < input1; i++) {
// 테스트 케이스가 시작될 때 마다 수첩 초기화
note1.clear();
note2.clear();
cin >> input2;
// 수첩1 값 받기
for (int j = 0; j < input2; j++) {
cin >> num;
note1.push_back(num);
}
// 수첩 1 오름차순 정렬
sort(note1.begin(), note1.end());
cin >> input3;
// 수첩2 값 받기
for (int j = 0; j < input3; j++) {
cin >> num;
note2.push_back(num);
}
// 수첩2의 첫 숫자부터 수첩1에 있는지 검사 (이분 탐색)
for (int j = 0; j < note2.size(); j++) {
left = 0;
right = note1.size() - 1;
while (left <= right) {
mid = (left + right) / 2;
// 탐색이 되었으면
if (note2[j] == note1[mid]) {
cout << 1 << endl;
break;
}
else if (note2[j] > note1[mid]) {
left = mid + 1;
continue;
}
else {
right = mid - 1;
continue;
}
}
// 탐색이 되지 않았으면
if (left > right)
cout << 0 << endl;
}
}
}
'알고리즘' 카테고리의 다른 글
C++ 알고리즘 - 15650 N과 M (3) (백트래킹) (0) | 2025.01.21 |
---|---|
C++ 알고리즘 - 4779 칸토어 집합 (0) | 2025.01.21 |
C++ 알고리즘 - 15650 N과 M (2) (백트래킹) (0) | 2025.01.20 |
C++ 알고리즘 - 15649 N과 M (1) (백트래킹) (0) | 2025.01.20 |
C++ 알고리즘 - 9655 돌 게임 (0) | 2025.01.20 |