
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int height, width, startBlock;
static int minHeight, maxHeight;
static int[][] board;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
height = Integer.parseInt(st.nextToken());
width = Integer.parseInt(st.nextToken());
startBlock = Integer.parseInt(st.nextToken());
board = new int[height][width];
minHeight = Integer.MAX_VALUE;
maxHeight = Integer.MIN_VALUE;
for (int y = 0; y < height; y++) {
st = new StringTokenizer(br.readLine());
for (int x = 0; x < width; x++) {
board[y][x] = Integer.parseInt(st.nextToken());
minHeight = Math.min(minHeight, board[y][x]);
maxHeight = Math.max(maxHeight, board[y][x]);
}
}
br.close();
int r_height = 0;
int r_time = Integer.MAX_VALUE;
for (int t_height = minHeight; t_height <= maxHeight; t_height++) {
int deleteBlock = 0; // 지운거
int addBlock = 0; // 쌓은거
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int current = board[y][x];
if (current > t_height) {
deleteBlock += current - t_height;
} else {
addBlock += t_height - current;
}
}
}
if ((deleteBlock + startBlock) >= addBlock) { // 가능
int need = (2 * deleteBlock) + addBlock;
if (r_time >= need) {
r_time = need;
r_height = Math.max(r_height, t_height);
}
}
}
bw.write(r_time + " " + r_height + "\n");
bw.close();
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 2178번: 미로 탐색 (0) | 2025.03.28 |
|---|---|
| [JAVA-자바] 21736번: 헌내기는 친구가 필요해 (0) | 2025.03.27 |
| [JAVA-자바] 7576번: 토마토 (0) | 2025.03.25 |
| [JAVA-자바] 1931번: 회의실 배정 (0) | 2025.03.25 |
| [JAVA-자바] 1074번: Z (0) | 2025.03.25 |