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 |
29 | 30 |
Tags
- 백트래킹
- Team Fortress 2
- 문자열
- DFS
- ue5
- c++
- 유니온 파인드
- VR
- XR Interaction Toolkit
- Unreal Engine 5
- 시뮬레이션
- 구현
- 다이나믹 프로그래밍
- 백준
- 누적 합
- 트리
- 그리디 알고리즘
- 정렬
- 브루트포스
- 우선순위 큐
- 투 포인터
- 다익스트라
- 그래프
- 자료구조
- BFS
- 알고리즘
- 유니티
- 스택
- 재귀
- 수학
Archives
- Today
- Total
1일1알
백준 5464번 주차장 C++ 본문
https://www.acmicpc.net/problem/5464
들어오는 차일 경우 : 빈자리가 있으면 그곳에 넣고, 없으면 대기 큐에 넣는다.
나가는 차일 경우 : 나가면서 총 가격을 계산해서 더하고, 만약 대기 큐가 비어있지 않다면 하나를 꺼내서 방금 나간 곳에 채워넣는다.
#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 |