알고리즘 공부

백준 10815번 숫자 카드 C++

빵어 2023. 10. 16. 20:27
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//https://www.acmicpc.net/problem/10815

bool MatchCards(int start, int end, int target, vector<int>& sanggeunCards) {
	while (start <= end) {
		int mid = (start + end) / 2;

		if (sanggeunCards[mid] == target)
			return true;

		if (sanggeunCards[mid] < target)
			start = mid + 1;
		else
			end = mid - 1;
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int n = 0, m = 0;
	cin >> n;

	vector<int> sanggeunCards(n);
	for (int i = 0; i < n; ++i)
		cin >> sanggeunCards[i];
	sort(sanggeunCards.begin(), sanggeunCards.end());

	int num = 0;
	cin >> m;
	for (int i = 0; i < m; ++i) {
		cin >> num;
		cout << MatchCards(0, n - 1, num, sanggeunCards) << " ";
	}
	return 0;
}

 

메모리 줄이려 애쓴 문제.. 

다른 분의 코드가 메모리를 줄이는 데에 도움이 많이 됐다

 

 

https://www.acmicpc.net/problem/10815