문제) 백준 - 이분 탐색 - 수열과 헌팅
https://www.acmicpc.net/problem/20495
20495번: 수열과 헌팅
예제에서, 수열의 수들을 차례로 x1, x2, x3이라 하자. -10 ≤ x1 ≤ 30, 25 ≤ x2 ≤ 55, 55 ≤ x3 ≤ 85이다. 만약 (x1, x2, x3) = (28, 25, 70)이라면, x2가 첫번째, x1이 두번째, x3이 세 번째 수가 된다. 또, (x1, x2
www.acmicpc.net
최대, 최소를 배열과 벡터에 저장합니다. 배열에 있는 수는 탐색할 숫자이며 벡터에 있는 수는 정렬을 하여 탐색할 범위입니다. 최솟값을 최댓값이 정렬된 벡터의 lower_bound, 최댓값을 최솟값이 정렬된 벡터의 upper_bound를 통해 인덱스를 구합니다.
C++ 소스코드)
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 <bits/stdc++.h> | |
#define endl "\n" | |
#define ll long long | |
#define INF 987654321 | |
#define MAX 500001 | |
using namespace std; | |
int N; | |
int small[MAX], big[MAX]; | |
vector<int> l, r; | |
signed main(){ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
cin >> N; | |
for(int i = 0; i < N; ++i){ | |
int a, b; cin >> a >> b; | |
small[i] = a - b; | |
big[i] = a + b; | |
l.push_back(a - b); | |
r.push_back(a + b); | |
} | |
sort(l.begin(), l.end()); | |
sort(r.begin(), r.end()); | |
for(int i = 0; i < N; ++i) | |
cout << lower_bound(r.begin(), r.end(), small[i]) - r.begin() + 1 << " " << upper_bound(l.begin(), l.end(), big[i]) - l.begin() << endl; | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 11655번 - ROT13 (C++) 문제 및 풀이 (0) | 2022.02.24 |
---|---|
[백준] 21317번 - 징검다리 건너기 (C++) 문제 및 풀이 (0) | 2022.02.23 |
[백준] 6996번 - 애너그램 (C++) 문제 및 풀이 (0) | 2022.02.23 |
[백준] 20294번 - 트리의 기둥과 가지 (C++) 문제 및 풀이 (0) | 2022.02.22 |
[백준] 11687번 - 팩토리얼 0의 개수 (C++) 문제 및 풀이 (0) | 2022.02.22 |
댓글