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 |
Tags
- 스택
- 수학
- 백트래킹
- 브루트포스
- ue5
- 유니온 파인드
- VR
- 다익스트라
- 우선순위 큐
- 재귀
- 그래프
- 정렬
- BFS
- 누적 합
- Team Fortress 2
- XR Interaction Toolkit
- Unreal Engine 5
- 트리
- 시뮬레이션
- 자료구조
- DFS
- c++
- 그리디 알고리즘
- 백준
- 유니티
- 투 포인터
- 다이나믹 프로그래밍
- 문자열
- 구현
- 알고리즘
Archives
- Today
- Total
1일1알
백준 2343번 기타 레슨 C++ 본문
이분 탐색으로 해결할 수 있는 문제이다.
최소를 동영상의 길이의 최소로 하고, 최대를 동영상의 길이들의 총 합으로 시작해서
이분탐색을 하면서 가장 적절한 블루레이의 크기를 구할 수 있다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int n, m;
int sum = 0;
int arr[100001] = { 0 };
bool IsPossible(int value) {
int cnt = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > value) return false;
sum += arr[i];
if (sum > value) {
sum = arr[i];
cnt++;
}
}
if (cnt < m) return true;
else return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
int low = arr[1];
int high = sum;
int ans = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (IsPossible(mid)) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1325번 효율적인 해킹 C++ (0) | 2021.12.14 |
---|---|
백준 2110번 공유기 설치 C++ (0) | 2021.12.13 |
백준 1743번 음식물 피하기 C++ (0) | 2021.12.11 |
백준 2302 극장 좌석 C++ (0) | 2021.12.10 |
백준 2251번 물통 C++ (0) | 2021.12.09 |