본문 바로가기

알고리즘/힙(우선순위큐)4

[백준] 11286번 절대값 힙 #Java 클래스 만들어줘서 풀이했다. compareTo만 알면 된다. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; class Point implements Comparable{ int n; public Point(int n){ this.n = n; } @Override public int compareTo(Point o) { if(Math.abs(this.n) == Math.abs(o.n)){ return this.n - o.n; } return Math.abs(this.n) - Math.abs(o.n); } } public class .. 2022. 5. 27.
[백준] 16435번 스네이크버드 #Java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()," "); int n = Integer.parseInt.. 2022. 5. 24.
[백준] 11279번 최대 힙 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Collections; import java.util.PriorityQueue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 가장 큰 수일수록 앞에 나온다. PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); int n = Integer.parseInt(br.read.. 2022. 5. 17.
[백준] 1927번 최소 힙 #Java import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); PriorityQueue pq = new PriorityQueue(); for (int i = 0; i < n; i++) { int num = Integer.parseInt(br.readLine()); if(num=.. 2022. 5. 16.