문제 링크입니다: https://www.acmicpc.net/problem/2776
2776번: 암기왕
연종이는 엄청난 기억력을 가지고 있다. 그래서 하루 동안 본 정수들을 모두 기억 할 수 있다. 하지만 이를 믿을 수 없는 동규는 그의 기억력을 시험해 보기로 한다. 동규는 연종을 따라 다니며,
www.acmicpc.net
간단한 이분 탐색 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
vector<int> v; | |
bool check(int target) | |
{ | |
int left = 0, right = v.size() - 1; | |
while (left <= right) | |
{ | |
int mid = (left + right) / 2; | |
if (v[mid] > target) | |
{ | |
right = mid - 1; | |
} | |
else if (v[mid] < target) | |
{ | |
left = mid + 1; | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int T; | |
cin >> T; | |
while (T--) | |
{ | |
v.clear(); | |
int N; | |
cin >> N; | |
v.resize(N); | |
for (int i = 0; i < N; i++) | |
{ | |
cin >> v[i]; | |
} | |
sort(v.begin(), v.end()); | |
int M; | |
cin >> M; | |
for (int i = 0; i < M; i++) | |
{ | |
int target; | |
cin >> target; | |
cout << check(target) << "\n"; | |
} | |
} | |
return 0; | |
} |

개발환경:Visual Studio 2022
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 7795번 먹을 것인가 먹힐 것인가 (0) | 2024.04.24 |
---|---|
백준 12094번 2048 (Hard) (1) | 2024.04.21 |
백준 13144번 List of Unique Numbers (0) | 2024.04.08 |
백준 14469번 소가 길을 건너간 이유 3 (0) | 2024.04.08 |
백준 2109번 순회강연 (0) | 2024.04.06 |