Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 유니온 파인드
- 수학
- c++
- 다이나믹 프로그래밍
- 다익스트라
- 트리
- 그래프
- 문자열
- Unreal Engine 5
- XR Interaction Toolkit
- 스택
- BFS
- 백준
- 유니티
- 알고리즘
- 우선순위 큐
- 구현
- 그리디 알고리즘
- 정렬
- 자료구조
- 백트래킹
- 브루트포스
- DFS
- 재귀
- 시뮬레이션
- 투 포인터
- 누적 합
- Team Fortress 2
- VR
- ue5
Archives
- Today
- Total
1일1알
백준 5464번 주차장 C++ 본문
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 |