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
- 그래프
- 코테
- 컴공과
- 알고리즘
- 개발
- vector
- 정석
- 오퍼레이팅시스템
- 너비우선탐색
- 문제풀이
- cs
- Stack
- bfs
- 백준
- 스택
- 자료구조
- 코딩
- c++
- Operating System
- 브루트포스
- 북리뷰
- coding
- 컴퓨터공학과
- 정석학술정보관
- Computer science
- 구현
- 컴공
- 오에스
- DP
- OS
Archives
- Today
- Total
Little Jay
[C++] 백준 1654번 - 랜선 자르기(retry needed) 본문
처음으로 이분 탐색을 써본 문제.
이분 탐색을 처음 써보는 거라 많이 어색하기도 하고,
구현을 어떻게 해야하는지 감이 잘 안와서
여러 블로그를 보고 조금 터득했다.
이분 탐색 문제를 더 풀어봐야 감이 올 것 같다.
이 문제도 내 힘으로 푼 것이 아니라 나중에 한번 더 풀어봐야겠다.
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
#define MAX 10000
int n, m;
long long int line_arr[MAX];
bool isPossible(long long line) {
int count = 0;
for (int i = 0; i < n; i++) {
count += line_arr[i] / line;
}
if (count >= m)
return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
long long high = 0, low = 1, answer = 0;
for (int i = 0; i < n; i++) {
cin >> line_arr[i];
high = max(high, line_arr[i]);
}
while (low <= high) {
long long mid = (high + low) / 2;
if (isPossible(mid)) {
if (answer < mid) {
answer = mid;
}
low = mid + 1;
}
else {
high = mid - 1;
}
}
cout << answer << '\n';
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 2417번 - 정수 제곱근 (0) | 2021.08.08 |
---|---|
[C++] 백준 13777번 - Hunt The Rabbit (0) | 2021.08.08 |
[C++] 백준 1453번 - 피시방 알바 (0) | 2021.08.07 |
[C++] 백준 11719번 - 그래도 출력하기 2 (0) | 2021.08.07 |
[C++] 백준 15596번 정수 N개의 합 (0) | 2021.08.07 |
Comments