[BOJ 2512] ์์ฐ
1. ๋ฌธ์ : https://www.acmicpc.net/problem/2512
2. ํ์ด
์ด๋ถํ์ ์กฐ๊ฑด์ ๋น๊ตํด๊ฐ๋ฉฐ +-
3. ์ฝ๋
package baekjoon;
import java.util.*;
class Main {
private static int[] arr; // ์ง๋ฐฉ ์์ฐ
private static long answer = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // ์ง๋ฐฉ์ ์
arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
long M = sc.nextLong(); //์ด ์์ฐ
long left = 0;
long right = arr[N-1];
while (left <= right) {
long mid = (left + right) / 2; //๋ด์ผํ ์ธ๊ธ
long sum = 0; // ๋ชจ๋ ์ง๋ฐฉ ์ธ๊ธ ํฉ
for (int money : arr) {
if (money >= mid) sum += mid; //๋ด๋ผ๋ ์ธ๊ธ ๋ผ ์ ์๋ ์ง๋ฐฉ
else sum += money; //๋ชป๋ด๋ ์ง๋ฐฉ์ ๊ฐ์ง ์ต๋ ๋๋ง ๋
}
if (sum > M) { // ๋ด๋ผ๋ ์ธ๊ธ ๋ณด๋ค ๋ง์ ๊ฒฝ์ฐ -> ์ธ๊ธ์ ์ค์ฌ๋ณธ๋ค.
right = mid - 1;
} else { // ๋ด๋ผ๋ ์ธ๊ธ๋ณด๋ค ์ ์ ๊ฒฝ์ฐ -> ์ธ๊ธ์ ๋์ฌ์ ๋ ์ต๊ณ ์ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ๋๋ค.
left = mid + 1;
answer = Math.max(answer, mid);
}
}
System.out.println(answer);
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 2343] ๊ธฐํ ๋ ์จ (0) | 2023.09.05 |
---|---|
[BOJ 19637] IF๋ฌธ ์ข ๋์ ์จ์ค (0) | 2023.08.31 |
[LeetCode] Add Two Numbers (0) | 2023.07.13 |
[LeetCode] Remove Duplicates from Sorted List II (0) | 2023.07.12 |
[LeetCode] Linked List Cycle II (0) | 2023.07.10 |
์ต๊ทผ๋๊ธ