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
- UnrealEngine4
- Programmers
- UnrealEngine5
- 백준
- DirectX11
- NRVO
- RVO
- directx
- DeferredRendering
- const
- RootMotion
- 언리얼엔진5
- Frustum
- 팰린드롬 만들기
- IFileDialog
- C
- 프로그래머스
- UE5
- 줄 세우기
- GeeksForGeeks
- winapi
- 1563
- Unreal Engine5
- 오블완
- algorithm
- baekjoon
- C++
- 티스토리챌린지
- softeer
- 2294
Archives
- Today
- Total
Game Develop
[Algorithm]Baekjoon 6236번 :: 용돈 관리 본문
https://www.acmicpc.net/problem/6236
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_set>
#include <thread>
#include <atomic>
using namespace std;
int n, m;
int moneys[100001] = { 0 };
bool check(int withdrawnMoney)
{
int withdrawnCount = 0;
int curMoney = 0;
for (int i = 0; i < n; ++i)
{
int money = moneys[i];
if (curMoney < money) // 남은돈이 더 적으면
{
++withdrawnCount;
curMoney = withdrawnMoney - money;
}
else
{
curMoney -= money;
}
}
return withdrawnCount <= m;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
int maxNum = 0;
for (int i = 0; i < n; ++i)
{
cin >> moneys[i];
maxNum = max(maxNum, moneys[i]);
}
int left = maxNum;
int right = n * maxNum ;
int answer = 0;
while (left <= right)
{
int mid = (left + right) / 2;
if (check(mid))
{
right = mid - 1;
answer = mid;
}
else
{
left = mid + 1;
}
}
cout << answer;
}
|
cs |
이 문제에서 유의해야할 점은 인출을 할 때는 현재 돈을 전부 다시 입금후에 인출을 해야한다는 것이다.
즉, 만약 현재 300원이 필요한데 100원만 있다면, 100원을 두번 인출하면 되는게 아니라 반드시 300원을 한번 인출해야 한다는 것이다.
이렇기 때문에 문제에서 요구하는 최소한의 인출금액은, 주어진 날짜별 금액의 최대값이 된다.
최소한의 인출금액이 날짜별금액보다 작으면 성립이 안된다. 한번밖에 인출못하니까..
그래서 left를 날짜별금액의 최대값으로 맞춰놨다. right는 m이 1일경우 한번의 인출로 모든 날짜의금액을 커버해야되니까 n * maxNum(금액의 최대값)으로 했다.
이렇게하고 mid값 (문제에서 요구하는 값)을 이진탐색으로 왔다갔다하면서 끝날짜까지 인출횟수를 카운팅했을 때, m보다 이하이면 return true 시켜주면 된다.
문제에서 m과 반드시 일치하라고 했는데 어차피 횟수가 남으면 그만큼 그냥 인출입금 해버리면 되기때문에 상관없다.
'Algorithm > Baekjoon' 카테고리의 다른 글
[Algorithm]Baekjoon 10819번 :: 차이를 최대로 (0) | 2025.01.26 |
---|---|
[Algorithm]Baekjoon 1507번 :: 궁금한 민호 (0) | 2025.01.25 |
[Algorithm]Baekjoon 2352번 :: 반도체 설계 (0) | 2025.01.25 |
[Algorithm]Baekjoon 3020번 :: 개똥벌레 (0) | 2025.01.23 |
[Algorithm]Baekjoon 1939번 :: 중량제한 (0) | 2025.01.23 |