Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

dh-winternagi 님의 블로그

(10816) 숫자 카드 본문

백준 (C++)/Solve

(10816) 숫자 카드

dh-winternagi 2026. 4. 18. 14:18

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

단계별로 풀어보기

25단계(이분 탐색) 2번째

 

 

 

이미 집합과 맵 단계에서 푼 적 있는 문제지만 이분 탐색을 이용하면 더 빠르게 풀 수 있다.

upper_bound는 탐색값 x를 초과하는 첫 위치를 반환하고, lower_bound는 탐색값 x 이상인 첫 위치를 반환한다.

따라서 upper_bound-lower_bound를 하면 정렬된 벡터 내에서 x의 개수를 알 수 있다.

결과를 보면 맵을 사용했을 때보다 메모리와 시간이 훨씬 줄어든 것을 볼 수 있다.

 

 

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  
  int n, m;
  
  cin >> n;
  
  vector<int> v(n);
  
  for(int i=0;i<n;i++)  cin >> v[i];
  
  sort(v.begin(), v.end());
  
  cin >> m;
  
  while(m--){
    int t;
    
    cin >> t;
    
    cout << upper_bound(v.begin(), v.end(), t)-lower_bound(v.begin(), v.end(), t) << " ";
  }
  
  return 0;
}

'백준 (C++) > Solve' 카테고리의 다른 글

(2805) 나무 자르기  (0) 2026.04.18
(1654) 랜선 자르기  (0) 2026.04.18
(1920) 수 찾기  (0) 2026.04.18
(6549) 히스토그램에서 가장 큰 직사각형  (0) 2026.04.18
(11444) 피보나치 수 6  (0) 2026.04.18