1일1알

백준 5464번 주차장 C++ 본문

알고리즘

백준 5464번 주차장 C++

영춘권의달인 2023. 4. 28. 14:08

https://www.acmicpc.net/problem/5464

 

5464번: 주차장

시내 주차장은 1부터 N까지 번호가 매겨진 N개의 주차 공간을 가지고 있다. 이 주차장은 매일 아침 모든 주차 공간이 비어 있는 상태에서 영업을 시작하며, 하룻동안 다음과 같은 방식으로 운영

www.acmicpc.net

 

들어오는 차일 경우 : 빈자리가 있으면 그곳에 넣고, 없으면 대기 큐에 넣는다.

나가는 차일 경우 : 나가면서 총 가격을 계산해서 더하고, 만약 대기 큐가 비어있지 않다면 하나를 꺼내서 방금 나간 곳에 채워넣는다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n, m;

vector<int> price;
vector<int> carWeight;
vector<int> currParkingLot;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n >> m;
	price = vector<int>(n);
	currParkingLot = vector<int>(n, -1);
	carWeight = vector<int>(m);
	for (int i = 0; i < n; i++) cin >> price[i];
	for (int i = 0; i < m; i++) cin >> carWeight[i];
	int64 sum = 0;
	queue<int> waitQueue;
	for (int i = 0; i < m * 2; i++) {
		int carIdx;
		bool out = false;
		cin >> carIdx;
		if(carIdx < 0) {
			carIdx = -carIdx;
			out = true;
		}
		carIdx--;

		if (out) {
			int parkingIdx;
			for (int i = 0; i < n; i++) {
				if (currParkingLot[i] == carIdx) {
					parkingIdx = i;
					break;
				}
			}
			currParkingLot[parkingIdx] = -1;
			sum += carWeight[carIdx] * price[parkingIdx];
			if (!waitQueue.empty()) {
				int nextCar = waitQueue.front();
				waitQueue.pop();
				currParkingLot[parkingIdx] = nextCar;
			}
		}
		else {
			int emptyPlace = -1;
			for (int i = 0; i < n; i++) {
				if (currParkingLot[i] == -1) {
					emptyPlace = i;
					break;
				}
			}
			if (emptyPlace == -1) {
				waitQueue.push(carIdx);
			}
			else {
				currParkingLot[emptyPlace] = carIdx;
			}
		}
	}
	cout << sum;
};

'알고리즘' 카테고리의 다른 글

백준 4108번 지뢰찾기 C++  (0) 2023.04.30
백준 23843번 콘센트 C++  (0) 2023.04.29
백준 1448번 삼각형 만들기 C++  (0) 2023.04.26
백준 20006번 랭킹전 대기열 C++  (0) 2023.04.25
백준 21735번 눈덩이 굴리기 C++  (0) 2023.04.22