문제 (https://www.acmicpc.net/problem/10816)
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
upper_bound()와 lower_bound를 사용하여 푼 문제! 기억하기~~~
upper_bound: 처음으로 value 값을 초과하는 원소의 주소
lower_bound: value 값 보다 크거나 같은 첫 번째 원소의 주소
upper_bound 리턴 값 - lower_bound 리턴 값
0: value 값이 존재하지 않는 것
양수: value 값의 개수
[소스코드]
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
/* | |
BOJ 10816번: 숫자 카드 2 | |
2021-12-19 | |
*/ | |
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); | |
vector<int> v, ans; | |
int n, m, num; | |
cin >> n; | |
for(int i = 0 ; i < n ; i++) { | |
cin >> num; | |
v.push_back(num); | |
} | |
sort(v.begin(), v.end()); | |
cin >> m; | |
for(int i = 0 ; i < m ; i++) { | |
cin >> num; | |
auto upper = upper_bound(v.begin(), v.end(), num); | |
auto lower = lower_bound(v.begin(), v.end(), num); | |
cout << upper - lower << " "; | |
} | |
return 0; | |
} |

'ALGORITHM > BOJ' 카테고리의 다른 글
[BOJ] 2776번 암기왕 (C++) (0) | 2021.12.20 |
---|---|
[BOJ] 2343번 기타 레슨 (C++) (0) | 2021.12.20 |
[BOJ] 1654번 랜선 자르기 (C++) (0) | 2021.12.19 |
[BOJ] 2805번 나무 자르기 (C++) (0) | 2021.12.19 |
[BOJ] 2521번 예산 (C++) (0) | 2021.12.19 |