출처: https://www.acmicpc.net/problem/4716
항해커톤에 출전한 각 팀에게 풍선을 달아줘야 합니다. 풍선은 방 A와 방 B에 보관되어 있고, 풍선을 달아주는 사람은 한 번에 풍선 하나만 들고 이동할 수 있습니다. 각 팀에 달아줘야 하는 풍선의 수와 방 A, B로부터의 거리가 주어질 때, 모든 풍선을 달아주는데 필요한 이동 거리의 최솟값을 구하는 프로그램을 작성하세요.
입력 형식
첫째 줄에 팀의 수 N(1 ≤ N ≤ 1,000)과 방 A와 B에 보관된 풍선의 수 A, B가 주어집니다.
다음 N개 줄에는 각 팀에게 달아줘야 하는 풍선의 수 K와 방 A, B로부터의 거리 DA, DB가 주어집니다.
입력의 마지막 줄에는 0이 세 개 주어집니다.
출력 형식
각 테스트 케이스에 대해 모든 팀에게 풍선을 달아주기 위해 필요한 이동 거리의 최솟값을 출력합니다.
예제 입력 1
3 15 35
10 20 10
10 10 30
10 40 10
0 0 0
예제 출력 1
300
import java.io.;
import java.util.;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
if (N == 0 && A == 0 && B == 0) break;
int[][] teams = new int[N][3];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
teams[i] = new int[] {
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())
};
}
// A와 B의 거리 차이가 큰 순으로 정렬
Arrays.sort(teams, (a, b) -> ____);
long totalDistance = 0;
List<int[]> equalDistanceTeams = new ArrayList<>();
// 거리 차이가 있는 팀들 처리
for (int[] team : teams) {
int balloons = team[0];
int distA = ____; // 거리 A 가져오기
int distB = team[2];
if (distA > distB) { // B가 더 가까운 경우
if (B >= balloons) {
B -= balloons;
totalDistance += (long)distB * balloons;
} else {
totalDistance += (long)distB * B;
balloons -= B;
totalDistance += (long)____ * balloons;
B = 0;
}
} else if (distA < distB) { // A가 더 가까운 경우
if (A >= balloons) {
A -= balloons;
totalDistance += (long)distA * balloons;
} else {
totalDistance += (long)distA * A;
balloons -= A;
totalDistance += (long)distB * balloons;
A = 0;
}
} else {
equalDistanceTeams.add(team);
}
}
// 거리가 같은 팀들 처리
for (int[] team : equalDistanceTeams) {
totalDistance += (long)team[0] * team[1];
}
System.out.println(totalDistance);
}
}
}
빈칸1: O
정답: Integer.compare(Math.abs(b[1] - b[2]), Math.abs(a[1] - a[2]))
해설: A와 B의 거리 차이의 절댓값을 기준으로 내림차순 정렬하기 위해 Integer.compare와 Math.abs를 함께 사용합니다.
빈칸2: O
정답: team[1]
해설: team 배열에서 거리 A는 두 번째 요소(인덱스 1)에 저장되어 있습니다.
빈칸3: X
정답: distA
해설: B의 풍선을 모두 사용한 후 남은 풍선은 A에서 가져와야 하므로 A까지의 거리인 distA를 사용합니다.
-> 'B의 풍선을 모두 사용한 후' 라는 조건을 못 본거 같다.