트럭 주차 - 2979

Seongjin Jo·2023년 4월 1일
0

Baekjoon

목록 보기
7/51
post-thumbnail

문제

//입력
5 3 1
1 6
3 5
2 8
//출력
33

풀이

import java.util.*;

// 트럭 주차 - B2
public class ex2979 {
    static int first = Integer.MAX_VALUE;
    static int last = Integer.MIN_VALUE;
    static int a,b,c;

    public static int solution(int[][] truck , int[] arr){
        int answer=0;
        //가장 이른시간 , 가장 늦은 시간
        for(int i=0; i<3; i++){
            for(int j=0; j<2; j++){
                first = Math.min(first,truck[i][j]);
                last = Math.max(last,truck[i][j]);
            }
        }

        //주차 요금 구하기
        for(int i=first; i<last; i++){
            if(arr[i]==1) answer += a*arr[i];
            else if(arr[i]==2) answer += b*arr[i];
            else if(arr[i]==3) answer += c*arr[i];
        }
        return answer;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        int[][] truck = new int[3][2];
        int[] arr = new int[100]; //시간

        for(int i=0; i<3; i++){
            int st = sc.nextInt();
            int et = sc.nextInt();
            truck[i][0]=st;
            truck[i][1]=et;

            for(int j=st; j<et; j++) arr[j]++;
        }

        System.out.println(solution(truck,arr));
    }
}

우선 백준 브론즈 문제다,, 구현/시뮬레이션 문제인데 생각보다 많이 헤맸다,, 브론즈 문제인 걸 생각해보면 참 갈길이멀다. ㅎ

  1. 우선 시간은 1~100시간 까지 주어지므로 arr을 선언. truck은 무조건 3대가 입력되며 주차시작시간과 주차끝나는시간을 담는 int[3][2]로 선언.
  2. 해당 시간 arr에 한 트럭의 st,et 까지를 ++해서 해당 시간에 트럭이 몇대 있는지 알수있게 해놓는다.
  3. 트럭이 주차하기 시작하는 가장이른시간과, 트럭이 주차장을 빠져나가는 마지막시간을 구해서, 그 시간 동안을 for문으로 arr의 값만큼 트럭 대수 당 요금을 곱해준다.

0개의 댓글