Algorithm

[구현] 백준 2979 트럭주차 JAVA

EllaCoo 2024. 4. 4. 21:04
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class R_1_2979 {
    public static void main(String[] args) throws Exception{
        // [구현] 2979 트럭주차
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int a = Integer.parseInt(input[0]);
        int b = Integer.parseInt(input[1]);
        int c = Integer.parseInt(input[2]);

        int[][] arr = new int[3][100];
        int max = Integer.MIN_VALUE;
        for (int i = 0 ; i < 3 ; i++) {
            String[] temp = br.readLine().split(" ");
            int temp1 = Integer.parseInt(temp[0]);
            int temp2 = Integer.parseInt(temp[1]) - 1; // 떠난시간 - 1  !!

            max = Math.max(temp2, max);
            for (int j = 0 ; j < 100 ; j++){
                arr[i][j] = j+1 >= temp1 && j+1 <= temp2 ? 1 : 0;
            }
        }
        int result = 0;
        for (int i = 0 ; i <= max ; i++) {
            int cntCar = 0;
            for (int j = 0 ; j < 3 ; j++) {
                if (arr[j][i] == 1) {
                    cntCar++;
                }
            }
            switch (cntCar) {
                case 1:
                    result += a;
                    break;
                case 2:
                    result += 2 * b;
                    break;
                case 3:
                    result += 3 * c;
                    break;
            }
        }
        System.out.println(result);
    }
}

 

시간 복잡도 : O(n)