본문 바로가기

PS/Problems

[Java] CodeForces Hello 2022 B.Integer shop

맨 왼쪽값을 가지면서 코스트가 가장 적은것

맨 오른쪽 값을 가지면서 코스트가 가장 적은것

양쪽을 감싸는것들중 코스트가 가장 적은 것

 

세 가지를 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();
    }
}