알고리즘

C++ 알고리즘 - 2776 암기왕

마루설아 2025. 1. 20. 21:45

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;
		}		
	}
}