[백준] 5464번

Jeanine·2022년 11월 7일
0

baekjoon

목록 보기
120/120
post-thumbnail
post-custom-banner

💻 C++ 기반

주차장
https://www.acmicpc.net/problem/5464

✔️ 문제에 나와있는 대로 구현하면 됨
✔️ 차가 빠질 때 요금 산정


#include <iostream>
#include <queue>

#define MAXN 100
#define MAXM 2001

using namespace std;

int N, M;
bool isTaken[MAXN + 10];
int parkingFee[MAXN + 10];
int carsWeight[MAXM + 10];
int parkingPlace[MAXM + 10];

int getParkingPlace()
{
    for (int i = 1; i <= N; i++)
    {
        if (!isTaken[i])
        {
            return i;
        }
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    cin >> N >> M;
    for (int i = 1; i <= N; i++)
    {
        cin >> parkingFee[i];
    }
    
    for (int i = 1; i <= M; i++)
    {
        cin >> carsWeight[i];
    }
    
    int total = 0;
    queue<int> waitingQ;
    for (int i = 0; i < 2 * M; i++)
    {
        int input;
        cin >> input;
        if (input > 0)
        {
            int place = getParkingPlace();
            if (place != 0)
            {
                parkingPlace[input] = place;
                isTaken[place] = true;
            }
            else
            {
                waitingQ.push(input);
            }
        }
        else
        {
            int carNum = (-1) * input;
            int place = parkingPlace[carNum];
            total += parkingFee[place] * carsWeight[carNum];
            parkingPlace[carNum] = 0;
            isTaken[place] = false;
            
            if (!waitingQ.empty())
            {
                parkingPlace[waitingQ.front()] = place;
                isTaken[place] = true;
                waitingQ.pop();
            }
        }
    }
    cout << total;
    return 0;
}
profile
Grow up everyday
post-custom-banner

0개의 댓글