728x90
반응형
- 풀이 링크:
GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이
알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.
github.com
#include <iostream>
#include <algorithm> // fill_n, min,max, swap
//#include <map> // 중복 x
//#include <string> // getline
//#include <cstring> // memset, strok, strstr
#include <vector>
//#include <queue> // priority_queue<자료형, 구현체, 비교 연산자> (중복허용),
#include <set> // 트리, 중복 x , multiset
//#include <unordered_set>
//#include <cmath>
using namespace std; // lower_bound, upper_bound
/*
[백준 10816 c++ VO] 숫자 카드 2
문제: 숫자 있는지 출력
접근1: 완전탐색 -> 50만*50만 이어서 시간초과
접근2: 벡터 정렬 후 lower_bound ,upper_bound 함수의 차가 그 원소의 갯수
시간복잡도:
풀이:
//1.입력
//2.정렬
//3.이진탐색
개념:
//: lower_bound(a.begin(), a.end(), num); 찾는 값 이상 숫자의 시작 주소
// upper_bound(a.begin(), a.end(), num); 찾는 값 초과 숫자의 시작 주소
*/
int n,num,m;
vector<int> v;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
//1.입력
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
v.push_back(num);
}
//2.정렬
sort(v.begin(), v.end());
//3.이진탐색
cin >> m;
for (int i = 0; i < m; i++) {
cin >> num;
cout << upper_bound(v.begin(), v.end(), num) - lower_bound(v.begin(), v.end(), num) << " ";
}
return 0;
}
반응형
'Algorithm_BOJ(백준) > 이진탐색' 카테고리의 다른 글
[백준 2143 c++ VV] 두 배열의 합 (0) | 2023.02.09 |
---|---|
[백준 2805 c++ VVO] 나무 자르기 (0) | 2023.02.09 |
[백준 1920 c++ VOVO] 수 찾기 (0) | 2023.02.09 |
[백준 2417 c++ X] 정수 제곱근 (0) | 2023.01.24 |
[백준 10815 c++ VO] 숫자 카드 (0) | 2023.01.23 |