맨 왼쪽값을 가지면서 코스트가 가장 적은것
맨 오른쪽 값을 가지면서 코스트가 가장 적은것
양쪽을 감싸는것들중 코스트가 가장 적은 것
세 가지를 minL, minR, minLR로 각각 저장해가며 case work 해야한다.
정작 대회중에는 풀이에 실패했다...
import java.util.*;
import java.io.*;
public class q2 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++){
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int left = Integer.parseInt(st.nextToken());
int right = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
int[] minL = new int[]{left, right, cost};
int[] minR = new int[]{left, right, cost};
int[] minLR = new int[]{left, right, cost};
bw.write(String.valueOf(cost));
bw.newLine();
for (int j = 1; j < n; j++){
st = new StringTokenizer(br.readLine());
left = Integer.parseInt(st.nextToken());
right = Integer.parseInt(st.nextToken());
cost = Integer.parseInt(st.nextToken());
if (left < minL[0]) minL = new int[]{left, right, cost};
if (right > minR[1]) minR = new int[]{left, right, cost};
if (left == minL[0] && cost < minL[2]) minL = new int[]{left, right, cost};
if (right == minR[1] && cost < minR[2]) minR = new int[]{left, right, cost};
if (left == minL[0] && minR[1] == right){
if (right - left > minLR[1] - minLR[0] || cost < minLR[2]){
minLR = new int[]{left, right, cost};
}
}
if (minR[1] - minL[0] > minLR[1] - minLR[0]){
bw.write(String.valueOf(minL[2] + minR[2]));
}
else if (minR[1] - minL[0] < minLR[1] - minLR[0]){
bw.write(String.valueOf(minLR[2]));
}
else{
bw.write(String.valueOf(Math.min(minL[2] + minR[2], minLR[2])));
}
bw.newLine();
}
}
bw.flush();
bw.close();
}
}
'PS > Problems' 카테고리의 다른 글
[Java] 백준 1389번 케빈 베이컨 - BFS (0) | 2022.01.08 |
---|---|
[Java] 백준 1107번 리모컨 - 브루트포스 (0) | 2022.01.08 |
[Java] 백준 24040번 예쁜 케이크 - 정수론 (0) | 2022.01.01 |
[Java] 백준 1629번 곱셈 - 분할정복법 (0) | 2022.01.01 |
[Java] 백준 1167번 트리의 지름 (0) | 2022.01.01 |