Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
Tags
- 정석학술정보관
- 오에스
- OS
- 백준
- 개발
- 브루트포스
- 오퍼레이팅시스템
- 알고리즘
- 스택
- 컴공
- 북리뷰
- 컴퓨터공학과
- bfs
- Computer science
- cs
- 너비우선탐색
- c++
- 구현
- 코테
- DP
- vector
- 문제풀이
- 컴공과
- 코딩
- 그래프
- 정석
- Operating System
- 자료구조
- coding
- Stack
Archives
- Today
- Total
Little Jay
[C++] 백준 10816번 숫자 카드 2 본문
처음에는 무지성으로 풀다가 음수인 숫자를 못보고 잘못 풀었음을 느꼈다
음수를 받을 때 어떻게 하면 좋을지 좀 많이 고민했었는데
이분 탐색으로도 안풀려서
한시간 고민하다가 안되서 결국 구글링
C++의 algorithm에는 다양한 기능들이 있다
그 중 lower_bound, upper_bound라는 기능들이 있는데 이거를 활용하면
정말 간단하게 풀리는 문제였다
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, m, x, y;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
cin >> m;
vector<int> answer(m);
for (int i = 0; i < m; i++) {
cin >> y;
auto upper = upper_bound(v.begin(), v.end(), y);
auto lower = lower_bound(v.begin(), v.end(), y);
answer[i] = upper - lower;
}
for (int n : answer) {
cout << n << " ";
}
cout << "\n";
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 1920 수 찾기 (0) | 2021.07.18 |
|---|---|
| [C++] 백준 15829번 Hashing (0) | 2021.07.18 |
| [C++] 백준 5054번 주차의 신 (0) | 2021.07.16 |
| [C++] 백준 9012번 괄호 (0) | 2021.07.16 |
| [C++] 백준 1966번 프린터 큐 (0) | 2021.07.16 |
Comments