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
- 트라이 자료구조
- prg패턴
- queue
- session인증
- 이진탐색
- 타임아웃
- JPA
- 예외처리
- 벌크헤드패턴
- 캐시 스탬피드
- 다중 서버
- 베타락
- ddl-auto
- BFS
- 이분탐색
- Stack
- 알고리즘
- thundering herd
- 스택
- Entity Manager
- 슬라이스 테스트
- expired key
- 자바
- 외부 서비스 장애
- 비관적 락
- java
- DP
- 낙관적 락
- id생성
- 백준
Archives
- Today
- Total
Coding 01
백준[11286]번 : 절댓값 힙 ( JAVA ) 본문
https://www.acmicpc.net/problem/11286
11286번: 절댓값 힙
첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
Code
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t =Integer.parseInt(br.readLine());
PriorityQueue<Integer> queue = new PriorityQueue<Integer>((a,b) -> {
if(Math.abs(a) - Math.abs(b) == 0) {
return a-b;
}
else {
return Math.abs(a) - Math.abs(b);
}
}); // 정렬의 조건을 정해주었다
while(t-- > 0) {
int n = Integer.parseInt(br.readLine());
if(n != 0) {
queue.add(n);
}
else {
if(queue.isEmpty()) {
bw.write(0+"\n");
}
else {
bw.write(queue.poll()+"\n");
}
}
}
br.close();
bw.close();
}
}
절댓값의 조건을 어떻게 처리해야 하나 생각하다가 PriorityQueue의 정렬조건에 그냥 만들면 된다는 것을 알고 쉽게 해결했다.
'백준' 카테고리의 다른 글
백준[2748]번 : 피보나치 수 2 ( JAVA ) (2) | 2023.07.29 |
---|---|
백준[24416]번 : 알고리즘 수업 - 피보나치 수 1 ( JAVA ) (0) | 2023.07.27 |
백준[1927]번 : 최소 힙 ( JAVA ) (0) | 2023.07.25 |
백준[11279]번 : 최대 힙 ( JAVA ) (0) | 2023.07.23 |
백준[11660]번 : 구간 합 구하기 5 ( JAVA ) (0) | 2023.07.18 |