진용이네 주차타워


Software Expert Academy [9280] 진용이네 주차타워

문제보기
Alt text

풀이

  • price[] : 주차공간 별 단위 무게당 요금
  • weight[][0] : 자동차 별 차량의 무게
  • weight[][1] : 자동차 별 주차 위치
  • order[] : 차량들의 주차장 출입 순서
  • park[] : 주차현황
  • queue : 대기차량을 위한 큐

소스코드

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Solution {
    static int n, m, ans;
    static int[] price;
    static int[][] weight;
    static int[] order;
    static int[] park;
    static Queue<Integer> queue;
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
 
        for (int tc = 1; tc <= T; tc++) {
            n = sc.nextInt();
            m = sc.nextInt();
            price = new int[n];
            weight = new int[m][2];
            order = new int[2 * m];
            park = new int[n];
            queue = new LinkedList<>();
            ans = 0;
 
            for (int i = 0; i < n; i++) {
                price[i] = sc.nextInt();
            }
            for (int i = 0; i < m; i++) {
                weight[i][0] = sc.nextInt();
            }
            for (int i = 0; i < 2 * m; i++) {
                order[i] = sc.nextInt();
            }
 
            income(0);
            System.out.println("#" + tc + " " + ans);
        }
    }
 
    public static void income(int idx) {
        if (idx >= 2 * m) {
            return;
        }
        if (order[idx] < 0) {
            int carNum = (order[idx] * (-1)) - 1;
            int w = weight[carNum][0];
            int location = weight[carNum][1];
            int p = price[location];
            ans += (w * p);
            park[location] = 0;
 
            if (queue.size() != 0) {
                int waitingCar = queue.poll();
                park[location] = waitingCar;
                weight[waitingCar - 1][1] = location;
            }
        } else {
            int parkNum = -1;
            for (int i = 0; i < park.length; i++) { // 주차공간 확인
                if (park[i] == 0) {
                    parkNum = i;
                    break;
                }
            }
 
            if (parkNum < 0) { // 주차공간이 없다면
                queue.add(order[idx]); // 차량번호
            } else {
                if (queue.size() != 0) {
                    queue.add(order[idx]);
                    int waitingCar = queue.poll();
                    park[parkNum] = waitingCar;
                    weight[waitingCar - 1][1] = parkNum;
                } else {
                    park[parkNum] = order[idx];
                    weight[order[idx] - 1][1] = parkNum;
                }
            }
        }
        income(idx + 1);
    }
}