728x90
11651번: 좌표 정렬하기 2
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
11650:좌표정렬하기와 거의 유사한 문제.
이번에는 배열이 아니라 노드를 사용해봤다.
package 백준renew;
import java.io.*;
import java.util.*;
public class 실버5_11651_좌표정렬하기2 {
static class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws Exception{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<>() {
public int compare(Node o1, Node o2) {
if(o1.y > o2.y) {
return 1;
}else if(o1.y < o2.y) {
return -1;
}else {
if(o1.x > o2.x) {
return 1;
}else if(o1.x < o2.x) {
return -1;
}else {
return 0;
}
}
}
});
int N = Integer.parseInt(br.readLine());
for(int i=0; i<N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
pq.offer(new Node(x, y));
}
while(!pq.isEmpty()) {
Node n = pq.poll();
sb.append(n.x).append(" ").append(n.y).append('\n');
}
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
728x90
'코테 > Algorithm' 카테고리의 다른 글
[BOJ] 12761: 돌다리 (JAVA) (0) | 2024.03.07 |
---|---|
[BOJ] 5014: 스타트링크 (JAVA) (0) | 2024.03.05 |
[BOJ] 18870: 좌표 압축 (JAVA) (0) | 2024.03.03 |
[BOJ] 11650: 좌표 정렬하기 (JAVA) (0) | 2024.03.02 |
[BOJ] 16953: A ➡️ B (JAVA) (0) | 2024.03.01 |