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
- 페이지이동함수
- @published 프로퍼티 래퍼
- 세로모드끄기
- modal 관리
- @observedobject 프로퍼티 래퍼
- 페이지전환
- featured-sliced-design
- react modal
- 컴퓨터네트워크
- C++
- react상태관리라이브러리
- react fsd
- CSS
- react-router-dom
- 반응형 css
- BFS
- 리렌더링최적화
- LazyHGrid
- @environmentobject 프로퍼티 래퍼
- react hook
- zustand란
- 동기 함수 내에서 비동기 함수 호출
- navigationBar 숨기기
- LazyVGrid
- 리액트최적화
- 블로그업로드확인
- GridItem
- 비동기함수
- zustand
- 가로모드끄기
Archives
- Today
- Total
leebaek
[BOJ/10816번숫자카드2] c++ / binary search 본문
문제
주어진 카드의 개수를 출력하는 문제
문제풀이
1.주어진 카드를 정렬시킴
2.lower_idx에서 target이 들어갈 수 있는 왼쪽 인덱스 값을 구함
-if ( arr[mid] >= target ) en = mid; | arr[mid] = target과 같은 경우
3.upper_idx에서 target이 들어갈 수 있는 오른쪽 인덱스 값을 구함
-if ( arr[mid] <= target ) st = mid+1; | arr[mid] = target과 같은 경우
4.lower_idx - upper_idx 출력
코드
#include <iostream>
#include <algorithm>
using namespace std;
int N, M, t;
int arr[500005];
int lower_idx(int len, int target) {
int st = 0;
int en = len;
while( st < en ) {
int mid = ( st + en ) / 2;
if ( arr[mid] >= target )
en = mid;
else
st = mid + 1;
}
return st;
}
int upper_idx(int len, int target) {
int st = 0;
int en = len;
while( st < en ) {
int mid = ( st + en ) / 2;
if ( arr[mid] > target )
en = mid;
else
st = mid + 1;
}
return st;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for ( int i = 0; i < N; i++ ) cin >> arr[i];
sort(arr, arr+N);
cin >> M;
while(M--) {
cin >> t;
cout << upper_idx(N, t) - lower_idx(N, t) << ' ';
}
}
> upper_bound / lower_bound 사용
위의 upper_idx와 lower_idx의 기능과 동일
#include <iostream>
#include <algorithm>
using namespace std;
int N, M, t;
int arr[500005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for ( int i = 0; i < N; i++ ) cin >> arr[i];
sort(arr, arr+N);
cin >> M;
while(M--) {
cin >> t;
cout << upper_bound(arr, arr+N, t) - lower_bound(arr, arr+N, t) << ' ';
}
}
'BOJ_C++_PS' 카테고리의 다른 글
[BOJ/1072번게임] c++ / binary_search (0) | 2023.11.06 |
---|---|
[BOJ/2295번세수의합] c++ / binary search (0) | 2023.11.04 |
[BOJ/5427번불!] c++ / BFS (1) | 2023.10.30 |
[BOJ/17836번공주님을구해라!] c++ / BFS (1) | 2023.10.29 |
[BOJ/16948번데스나이트] c++ / BFS (1) | 2023.10.28 |