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 | 31 |
Tags
- java 프로그래머스
- leetcode
- 코딩테스트
- java leetcode
- 파이썬
- 자바 리트코드
- Java
- 자바 5464
- dfs
- 리트코드
- leetcode 1721
- 백준 18222
- 코테
- 분할정복
- 리트코드 1557
- 자바
- 인텔리제이 에러
- DP
- 그래프 자바
- 리트코드 자바
- 구현
- 백준 16935
- 스프링 에러
- 프로그래머스
- BFS
- daily challenge
- 프로그래머스 java
- 카카오
- 백준
- 스택
Archives
- Today
- Total
레벨업 일지
[Java] 백준 5464.주차장 본문
문제
https://www.acmicpc.net/problem/5464
5464번: 주차장
시내 주차장은 1부터 N까지 번호가 매겨진 N개의 주차 공간을 가지고 있다. 이 주차장은 매일 아침 모든 주차 공간이 비어 있는 상태에서 영업을 시작하며, 하룻동안 다음과 같은 방식으로 운영
www.acmicpc.net
풀이
내가 생각한 문제에서 요구사항은 다음과 같다.
주어진 조건대로 waiting Q를 구현하고싶은데 우선순위를 수정할줄 아느냐?
구현 문제를 풀때, 의미가 있는 변수 이름으로 설정해주면 내 코드를 보더라도 헷갈리지 않고 풀이할수 있다.
주석을 사용하는 대신 변수 네이밍으로 뜻만 보더라도 알아볼 수 있게 하였다.
풀이 알고리즘은 다음과 같다.
- 현재 차가 들어올 타임일때
- 현재 대기 번호 뽑은 차량이 존재하거나 주차 공간이 없어 대기 번호를 발급 받아야할때
- 대기 번호를 발급한다.
- 주차장을 사용하고 이것을 기록한다
- 현재 대기 번호 뽑은 차량이 존재하거나 주차 공간이 없어 대기 번호를 발급 받아야할때
- 현재 차가 나갈때
- 요금 정산을 한다.
- 대기 번호가 존재하면, 대기 번호에서 하나 뽑아 차량을 주차시킨다.
이해하기 쉽게 코드에 경로 주석을 적었다.
코드
import java.awt.*;
import java.io.*;
import java.util.*;
public class Main {
PriorityQueue<int[]> Empty_Position_Q;
LinkedList<Integer> Car_Waiting_Q;
int m[][];
int cost[];
int weight[];
int info[];
int parkIndex[];
int size;
int ans;
BufferedWriter bw;
public static void main(String args[]) throws IOException {
Main m = new Main();
m.solution();
}
public void solution() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
String str = br.readLine();
int N = Integer.parseInt(str.split(" ")[0]);
int M = Integer.parseInt(str.split(" ")[1]);
cost = new int[N+1];
weight = new int[M+1];
info = new int[2*M];
parkIndex = new int[M+1];
ans = 0;
size = 0 ;
for(int i =0 ; i< N;i++) {
cost[i] = Integer.parseInt(br.readLine());
}
for(int i =1 ; i<= M;i++) {
weight[i] = Integer.parseInt(br.readLine());
}
for(int i =0 ; i< 2*M;i++) {
info[i] = Integer.parseInt(br.readLine());
}
Empty_Position_Q = new PriorityQueue<>((a,b)->a[0]-b[0]);
Car_Waiting_Q = new LinkedList<>();
for(int i =0 ;i < N;i++){
Empty_Position_Q.offer(new int[]{i,cost[i]});
}
int t;
int idx;
int c[];
int temp = 0;
for(int i =0 ;i < 2*M; i++){
if(info[i] > 0){ //1
if(!Car_Waiting_Q.isEmpty() ||Empty_Position_Q.isEmpty()){//1-1
Car_Waiting_Q.offer(info[i]);//1-1-1
}else{//1-1-2
c = Empty_Position_Q.poll();
parkIndex[info[i]] = c[0];
}
}else{//2
t = info[i] * (-1);
idx = parkIndex[t];
parkIndex[t] = 0;
Empty_Position_Q.offer(new int[]{idx, cost[idx]});
ans += cost[idx] * weight[t];//2-1
if(!Car_Waiting_Q.isEmpty()){//2-2
c = Empty_Position_Q.poll();
parkIndex[Car_Waiting_Q.poll()] = c[0];
}
}
}
bw.write(Integer.toString(ans));
bw.close();
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[Java] 백준 16935 배열 돌리기 3 (0) | 2023.06.29 |
---|---|
[Java] 백준 10799 쇠막대기 (0) | 2023.06.29 |
[Java] 백준 1992. 쿼드트리 (0) | 2023.06.27 |
[Java] 백준 2573.빙산 (0) | 2023.05.15 |
[Java] 백준 16236. 아기 상어 (0) | 2023.05.15 |
Comments