[BOJ] 2512 예산

Time Lapse :20min 3sec

2512.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int budget[10000];
int main(int argc, char** argv) {
int N, M, left = 0, right = 0 , mid, c, answer = 0;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &budget[i]);
right = right > budget[i] ? right : budget[i];
}
scanf("%d", &M);
while (left <= right) {
mid = (left + right) / 2;
c = 0;
for (int i = 0; i < N; i++) {
budget[i] > mid ? (c += mid) : (c += budget[i]);
if (c > M) { right = mid - 1; break; }
}
if (c <= M) {
left = mid + 1;
answer = answer > mid ? answer : mid;
}
}
printf("%d", answer);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2512/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.