• 문제 링크
25945번: 컨테이너 재배치
항구의 컨테이너 하치장 바닥에는 컨테이너를 쌓을 수 있는 칸이 일렬로 총 $n$개가 그려져 있고, 현재 하치장에는 총 $m$개의 컨테이너가 쌓여 있다. 개별 컨테이너의 높이는 모두 $1$로 동일하며
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
int[] arr = new int[n];
int sum = 0;
while (n-- > 0) sum += arr[n] = read();
int a = 0, b = 0, avg = sum / arr.length;
for (int i : arr) {
if (i > avg) a += i - avg - 1;
else b += avg - i;
}
bw.write(String.valueOf(Math.max(a, b)));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 12755 수면 장애 - Brute Force / Java (0) | 2023.11.24 |
---|---|
[백준] 25426 일차함수들 - Greedy / Java (0) | 2023.11.23 |
[백준] 29767 점수를 최대로 - Greedy / Java (0) | 2023.11.21 |
[백준] 30457 단체줄넘기 - Greedy / Java (0) | 2023.11.20 |
[백준] 14471 포인트 카드 - Greedy / Java (0) | 2023.11.19 |
댓글