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
- YBMCOS
- dp
- java
- 취득후기
- 01BFS
- 백준
- 다이나믹프로그래밍
- 우선순위큐
- 게더타운시작
- 이젠 골드구현도 어렵네..
- PS
- COSPROJAVA1급
- QUICKSTARTGUIDE
- 엘라스틱서치
- 재귀함수
- 구현
- 알고리즘
- 시뮬레이션
- 완전탐색
- 다익스트라
- deque
- spring
- 세그먼트트리
- DFS
- COSPRO
- BFS
- 네트워크플로우
- 자바PS
- 백준코딩테스트
- GatherTown
Archives
- Today
- Total
공부공간
BOJ - 7662 ) 이중 우선순위 큐 본문
https://www.acmicpc.net/problem/7662
솔직히 우선순위큐 2개로 풀려했지만 실패했다.. 시간초과가 계속뜨길래 풀이를 참고하니
Treemap으로 간단하게 푸는 풀이가 대세였다..
Treemap에 대한설명은 구글에 잘 되어있는 자료가 많지만
간단하게 key는 오름차순으로 정렬되며 중간 삭제나 lastget , firstkey로 처음과 끝을 가져 올 수 있는 편리한
자료구조이다.
Hashmap 저격데이터가 들어있는경우가 많아서 map자료구조는 생각못했는데 자주이용해봐야겠다..
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.io.BufferedReader;
public class BOJ_7662 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
int tc = Integer.parseInt(st.nextToken());
for(int t=0;t<tc;t++) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
TreeMap<Integer, Integer> tm = new TreeMap<>();
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine());
char c = st.nextToken().charAt(0);
int m = Integer.parseInt(st.nextToken());
if(c=='I') {
if(tm.getOrDefault(m, 0)==0) {
tm.put(m, 1);
} else {
tm.put(m, tm.get(m)+1);
}
} else {
if(tm.keySet().size()==0) {
continue;
}
if(m==1){
int l = tm.lastKey();
if(tm.get(l) > 1) {
tm.put(l, tm.get(l)-1);
} else {
tm.remove(l);
}
} else {
int f = tm.firstKey();
if(tm.get(f) > 1) {
tm.put(f, tm.get(f)-1);
} else {
tm.remove(f);
}
}
}
}
if(tm.keySet().size()==0) {
sb.append("EMPTY\n");
} else {
sb.append(tm.lastKey()+" "+tm.firstKey()+"\n");
}
}
System.out.println(sb.toString());
}
}
'알고리즘 > 구현,시뮬' 카테고리의 다른 글
BOJ - 17219 ) 비밀번호찾기 (0) | 2022.03.04 |
---|---|
BOJ - 1676 ) 팩토리얼 0의 개수 (0) | 2022.03.03 |
BOJ - 1475 ) 방번호 (0) | 2022.02.27 |
BOJ - 3020 ) 개똥벌레 (1) | 2021.11.03 |
BOJ - 5373 ) 큐빙 (0) | 2021.10.02 |
Comments